We have used UNIX or Linux systems, not unfamiliar with tools such as Vi,sed,grep,awk, you should know the regular expressions and metacharacters for the delimiter search pattern. So what is a regular expression? A regular expression is a sequence or character pattern that is responsible for string matching of text content when searching or replacing text. Simple regular expressions consist directly of a string or character set to be matched.
Regular expressions are typically delimited by a slash (/). Virtually any string can be a delimiter. For example:/abc/, ABC?
Expression modifiers and simple statements
Common modifiers: If unless while until foreach
If condition modifier
Format Expression2 If Expression1 if the Expression1 expression is true, the Expression2 expression content is executed.
Demo
(1) $x = 5;
Print $x if x==5; ====> Output 5
(2) $_= "xabcy\n";
Print if/abc/; ====> Output Xabcy
(3) $_= "I lost my gloves in the clover.";
Print "Found Love in gloves!\n" if/love/; ====> output found love in gloves!
Unless modifier
Format Expression2 unless Expression1 if the Expression1 is false, the Expression2 expression content is executed.
Demo
(1) $x = 5;
Print $x unless $x ==6; ====> Output 5
While loop modifier
Format Expression2 while Expression1 when the first expression is true, the while loop modifier executes the second expression repeatedly.
Demo
(1) $x = 1;
Print $x + +, "\ n" while $x!=5; ====> Output 1,2,3,4
Until modifier
Format Expression2 until Expression1 as long as the first expression is false, the while loop modifier repeats the second expression
Demo
(1) $x = 1;
Print $x + +, "/n" until $x ==5; ====> Output 1,2,3,4
foreach modifier
The values of each element in the list are judged one by one, and the individual list elements are referenced by scalar $_.
Demo
(1) @alpha = (A.. z, "\ n");
Print foreach @alpha; ====>abcdefghijkmnopqrstuvwxyz
Regular-expression operators
M operator and match
Demo
(1) while (<DATA>) {
Print if/betty/====> output Betty Boop
}
__data__
Steve Blenheim
Betty Boop
Igor Chevsky
Norma Cord
(2) while (<DATA>) {
print if m#jon#; ====> output Jon DeLoach
}
__data__
Steve Blenheim
Betty Boop
Igor Chevsky
Norma Cord
Jon DeLoach
G Global Modifier
I modifier: case insensitive
Demo
(1) $_= "I lost my gloves in the clover,love.";
@list =/love/gi;
Print "@list. \ n "; ====>love Love Love
The X-expression modifier is used to place comments or white-space characters in a regular expression so that the meaning of the regular expression is more explicit.
Demo
(1) $_= "San Francisco to Hong Kong \ n";
/francisco
/x;
Print "Comments and spaces were removed and \$& is $&\n"; ====> output comments and spaces were removed and $& is Francisco
s operator vs. replace
Demo
(1) $_= "Knock at Heaven ' s door. \ n ";
s/knock/"Knock," * * *. Knocking "/EI;
Print "He ' s $_"; ====>he ' s knock,knock,knocking at heaven ' s door.
Pattern-binding operators
=~ !~
Demo
(1) while ($_=<data>) {
Print $_if $_=~/igor/; ====> output Igor Chevsky
}
__data__
Steve Blenheim
Betty Boop
Igor Chevsky
Norma Cord
(2) while ($_=<data>) {
Print $_ if $_!~/igor/; ====> Enter a name other than Igor Chevsky
}
__data__
Steve Blenheim
Betty Boop
Igor Chevsky
Norma Cord
The regular expression of Perl--pattern matching