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

Perl 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

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

snippet
$line = "This is javaTpoint.";
if ($line =~ /java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}
if ($line =~ /Java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}

Output:

Output
Matching Not Matching

Perl Matching Operator !~

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

snippet
$ line = "This is javaTpoint.";
if ($line!~ /java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}
if ($line!~ /Java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}

Output:

Output
Not Matching Matching

Perl Matching Operator with $_

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

snippet
$_ = "This is javaTpoint.";
    if (/java/) {
        print "Matching\n";
    }
    else {
        print "Not Matching\n";
    }
if (/Java/) {
        print "Matching\n";
    }
    else {
        print "Not Matching\n";
    }

Output:

Output
Matching Not Matching

Perl Matching Operator with m

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

snippet
$ line = "This is javaTpoint.";
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:

Output
Matching Not Matching

Perl Matching Operator with $1, $2...

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

snippet
my $word = "CuNaHg";
    $word =~ /(((Cu)(Na))(Hg))/;
    print "1: $1 2: $2 3: $3 4: $4 5: $5 6: $6\n";

Output:

Output
1: CuNaHg 2: CuNa 3: Cu 4: Na 5: Hg 6:

Perl Matching Operator with ?

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

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:

Output
Cu Na Hg

Perl Substitution Operator

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

Its basic syntax is:

snippet
s/oldPattern/newPattern /;

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:

Output
solid will remain liquid until it is evaporated solid will remain solid until it is evaporated

Perl Translation Operator

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

Its basic syntax is:

snippet
tr/oldLetter/newLetter /;

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:

Output
ziquid wizz remain ziquid untiz it is evaporated

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:

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