Perl Regular Exprssion

  • Regular expressions allow you to match text based on patterns. 
  • Perl regular expressions are powerful and very simple to use. 
  • The perl regular expression syntax is quite similar with that of awk, grep and sed.
  • There are three regular expression operators inside perl:
    1) Matching regular expression - m//
    2) Substitute regular expression - s///
    3) Transliterate regular expression - tr///
  • The forward slashes in each case act as delimiters for the regular expression (regex) that you are specifying. 
  • If you are comfortable with any other delimiter, then you can use in place of forward slash.
Matching regular expression operator:
The match operator, m//, is used to match a string or statement to a regular expression.

The following illustrates the basic syntax of regular expression matching:
1
string =~ regex;

The operator =~ is the binding operator. 

The whole expression returns a value to indicate whether the regular expression regex was able to match the string successfully.

Example:
1) Matching operator =~ is used to match a word in the given string.It will set $true to 1 if $str matches the regex, or 0 if the match fails.
1
2
3
4
5
6
7
8
#!/usr/bin/perl

$str = "World is beautiful";
if ($str =~ /utif/) {
   print "matching\n";
} else {
   print "not matching\n";
}
Output: Matching

2) It is the opposite of (=~). If the letters match it gives the output as not matched and vice versa.
1
2
3
4
5
6
7
8
#!/usr/bin/perl

$str = "World is beautiful";
if ($str !~ /utif/){  
   print "Matching\n";  
}else{  
   print "Not Matching\n";  
}  
Output: Not Matching

3) Perl Matching Operator with m.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ str = "This is Perl tutorials.";  
if ($str =~ m[toria]){  
   print "Matching\n";  
}else{  
   print "Not Matching\n";  
}  

if ($str =~ m{Java}){  
   print "Matching\n";  
}else{  
   print "Not Matching\n";  
}  

if ($str =~ m /Perl/){  
   print "Matching\n";  
}else{  
   print "Not Matching\n";  
}  
Output:
Matching
Not Matching
Matching

Substitution Operator
The substitution operator, s///, is an extension of the match operator that replace the text matched with some new text.
Here we are replacing London with Dublin in the first part with s///.

In the second part, 'London' is replaced with 'Dublin' globally with s///g.


Example:
1
2
3
4
5
6
7
8
$str = "London is the one of the biggest city in Europe. London is very cold in December.";  

$str =~ s/London/Dublin/;  
print "$str\n";  

$str = "London is the one of the biggest city in Europe. London is very cold in December.";  
$str =~ s/London/Dublin/g;  
print "$str\n";  
Output:
Dublin is the one of the biggest city in Europe. London is very cold in December.
Dublin is the one of the biggest city in Europe. Dublin is very cold in December.

Translation Operator 
The translation replaces all occurrences of the characters in search list with the corresponding characters in replacement list. 

Example: 
Here, all the 'l' and 'i' letters will be replaced with 'z' and 'x' letters by translation operator.
1
2
3
$str = "liquid will remain liquid until it is evaporated.";  
$str =~ tr/li/zx/;  
print "$str\n";  
Output: zxquxd wxzz remaxn zxquxd untxz xt xs evaporated.

Matching operator modifier
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



<-- Previous || Next -->

2 comments: