A regular expression is a string of characters that defines a specific pattern. The perl regular expression syntax is quite similar with that of awk, grep and sed.
There are three regular expression operators inside perl:
Perl matching operators have various modifiers. It is mainly used to match a string or statement to a regular expression.
Operators | Description |
---|---|
cg | Continue search even if the global match fails |
g | Search globally for all matches |
i | Search the match with case insensitivity |
m | If string has a new line character, the $ and ^ will match against a new line boundary instead of string boundary |
o | Allow expression evaluation only once |
s | Use . to match a new line character |
x | Use white space in the expression |
=~
- Matching OperatorThe matching operator =~ is used to match a word in the given string. It is case sensitive, means if string has a lowercase letter and you are searching for an uppercase letter then it will not match.
$line = "This is rookienerd."; if ($line =~ /java/){ print "Matching\n"; }else{ print "Not Matching\n"; } if ($line =~ /Java/){ print "Matching\n"; }else{ print "Not Matching\n"; }
!~
- Matching OperatorIt is the opposite of the earlier one (=~). If the letters match it gives the output as not matched and vice versa.
$ line = "This is rookienerd."; if ($line!~ /java/){ print "Matching\n"; }else{ print "Not Matching\n"; } if ($line!~ /Java/){ print "Matching\n"; }else{ print "Not Matching\n"; }
$_
- Matching OperatorYou can also match it against a special default variable $_.
$_ = "This is rookienerd."; if (/java/) { print "Matching\n"; } else { print "Not Matching\n"; } if (/Java/) { print "Matching\n"; } else { print "Not Matching\n"; }
m
- Matching OperatorThe matching operator m is also used to match a word in the given string.
$ line = "This is rookienerd."; if ($line=~ m[java]){ print "Matching\n"; }else{ print "Not Matching\n"; } if ($line=~ m{Java}){ print "Matching\n"; }else{ print "Not Matching\n"; }
$1, $2...
- Matching OperatorThe $1, $2 will print the word according to the specified bracket.
my $word = "CuNaHg"; $word =~ /(((Cu)(Na))(Hg))/; print "1: $1 2: $2 3: $3 4: $4 5: $5 6: $6\n";
?
- Matching OperatorIt prints the matched character inside the bracket from a given string.
my $word = "CuNaHg"; $word =~ /(?:(Cu)NaHg)/; print "$1\n"; # prints "Cu" $word =~ /(?:Cu(Na)Hg)/; print "$1\n"; # prints "Na" $word =~ /(?:CuNa(Hg))/; print "$1\n"; # prints "Hg?
The substitution operator is just an extension of the matched operator. It allows the replacement of text matched with some new text.
s/oldPattern/newPattern /;
Here we are replacing liquid with solid in the first part with s///.
In the second part, 'liquid' is replaced with 'solid' globally with s///g.
$line = "liquid will remain liquid until it is evaporated"; $line =~ s/liquid/solid/; print "$line\n"; print"\n"; $line = "liquid will remain liquid until it is solidified"; $line =~ s/liquid/solid/g; print "$line\n";
Translation operator is similar as substitution operator. But translation does not use regular expression for search on replacement values.
tr/oldLetter/newLetter /;
Here, all the 'l' letters will be replaced with 'z' letters by translation operator.
$line = "liquid will remain liquid until it is evaporated"; $line =~ tr/l/z/; print "$line\n";
Here, all the 'l' and 'i' letters will be replaced with 'z' and 'x' letters by translation operator.
$line = "liquid will remain liquid until it is evaporated"; $line =~ tr/li/zx/; print "$line\n";