Perl Regular Expression

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:

  • Matching regular expression operator
  • Substitute regular expression operator
  • Transliterate regular expression operator

Matching Operators

Perl matching operators have various modifiers. It is mainly used to match a string or statement to a regular expression.

Matching Operator Modifiers
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 Operator

The 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.

Example
snippet
$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";
}
Output
Matching Not Matching
!~ - Matching Operator

It is the opposite of the earlier one (=~). If the letters match it gives the output as not matched and vice versa.

Example
snippet
$ 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";
}
Output
Not Matching Matching
$_ - Matching Operator

You can also match it against a special default variable $_.

Example
snippet
$_ = "This is rookienerd.";
    if (/java/) {
        print "Matching\n";
    }
    else {
        print "Not Matching\n";
    }
if (/Java/) {
        print "Matching\n";
    }
    else {
        print "Not Matching\n";
    }
Output
Matching Not Matching
m - Matching Operator

The matching operator m is also used to match a word in the given string.

Example
snippet
$ 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";
}
Output
Matching Not Matching
$1, $2... - Matching Operator

The $1, $2 will print the word according to the specified bracket.

Example
snippet
my $word = "CuNaHg";
    $word =~ /(((Cu)(Na))(Hg))/;
    print "1: $1 2: $2 3: $3 4: $4 5: $5 6: $6\n";
Output
1: CuNaHg 2: CuNa 3: Cu 4: Na 5: Hg 6:
? - Matching Operator

It prints the matched character inside the bracket from a given string.

Example
snippet
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?
Output
Cu Na Hg

Substitution Operator

The substitution operator is just an extension of the matched operator. It allows the replacement of text matched with some new text.

Syntax
s/oldPattern/newPattern /;
Example

Perl Substitution Operator with s///

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.

snippet
$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";
Output
solid will remain liquid until it is evaporated solid will remain solid until it is evaporated

Translation Operator

Translation operator is similar as substitution operator. But translation does not use regular expression for search on replacement values.

Syntax
tr/oldLetter/newLetter /;
Example #1

Perl Translation Operator replacing one letter

Here, all the 'l' letters will be replaced with 'z' letters by translation operator.

snippet
$line = "liquid will remain liquid until it is evaporated";
$line =~ tr/l/z/;
print "$line\n";
Output
ziquid wizz remain ziquid untiz it is evaporated
Example #2

Perl Translation Operator replacing more than one letter

Here, all the 'l' and 'i' letters will be replaced with 'z' and 'x' letters by translation operator.

snippet
$line = "liquid will remain liquid until it is evaporated";
$line =~ tr/li/zx/;
print "$line\n";
Output
zxquxd wxzz remaxn zxquxd untxz xt xs evaporated
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +