The regex house(cat|keeper) means match house followed by either cat or keeper. Parts of a regex are grouped by enclosing them in parentheses. The grouping metacharacters () allow a part of a regex to be treated as a single unit. # Grouping things and hierarchical matching Here, all the alternatives match at the first string position, so the first matches. "cats" =~ /cats|cat|ca|c/ # matches "cats"Īt a given character position, the first alternative that allows the regex match to succeed will be the one that matches. "cats and dogs" =~ /dog|cat|bird/ # matches "cat"Įven though dog is the first alternative in the second regex, cat is able to match earlier in the string. Some examples: "cats and dogs" =~ /cat|dog|bird/ # matches "cat" If cat doesn't match either, then the match fails and Perl moves to the next position in the string. If dog doesn't match, Perl will then try the next alternative, cat. At each character position, Perl will first try to match the first alternative, dog. As before, Perl will try to match the regex at the earliest possible point in the string. ![]() ![]() To match dog or cat, we form the regex dog|cat. We can match different character strings with the alternation metacharacter '|'. "Hello World" =~ m /x # matches the whole string # Matching this or that If you're matching against $_, the $_ =~ part can be omitted: $_ = "Hello World" įinally, the // default delimiters for a match can be changed to arbitrary delimiters by putting an 'm' out front: "Hello World" =~ m!World! # matches, delimited by '!' Perl code is succinct and can be written quickly, while still performing well in most cases. Print "It matches\n" if "Hello World" =~ /$greeting/ Perl is a flexible, feature-rich, dynamic programming language. The literal string in the regex can be replaced by a variable: $greeting = "World" The sense of the match can be reversed by using !~ operator: print "It doesn't match\n" if "Hello World" !~ /World/ This idea has several variations.Įxpressions like this are useful in conditionals: print "It matches\n" if "Hello World" =~ /World/ In our case, World matches the second word in "Hello World", so the expression is true. Our Perl tutorial includes all topics of Perl such as installation, example, operator, operator type, control statement, loop,comments, arrays, string, escaping characters, namespace, hashes, regular. The operator =~ associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match. Theres a general delimited string and regex quoting syntax similar to Perls. Perl is a cross-platform environment and library for running JavaScript applications which is used to create network and server-side applications. In this statement, World is a regex and the // enclosing /World/ tells Perl to search a string for a match. A regex consisting of a word matches any string that contains that word: "Hello World" =~ /World/ # matches The simplest regex is simply a word, or more generally, a string of characters. This page assumes you already know things, like what a "pattern" is, and the basic syntax of using them. This page covers the very basics of understanding, creating and using regular expressions ('regexes') in Perl. ![]() Perlrequick - Perl regular expressions quick start #DESCRIPTION Grouping things and hierarchical matching.next – learns how to control the flow of the loop.do…until – execute a block of code repeatedly with the test condition checked at the end of each iteration.until – learns how to execute a block of code as long as a condition is true.do…while – learns how to execute a block of code as long as the.while – shows you how to execute a block of code based on a condition.for loop – learns how to iterate over elements of a list.given…when – introduces the given…when statement that allows you to match a value or variable against a list of values.unless – provides you with another statement to execute a block of code based on a condition.if…else – learns how to use the if…else statement to execute a block of code based on a specified condition.In addition, you’ll learn how to use a relatively new statement in Perl called given/when statement, which is similar to the switch case statement in C. In this section, you’ll learn how to use Perl control flow statements including if/elsif/else, for/foreach, while/do while, until/do until, next,last statements.
0 Comments
Leave a Reply. |