Perl | Operators in Regular Expression
Prerequisite: Perl | Regular Expressions The Regular Expression is a string which is the combination of different characters that provides matching of the text strings. A regular expression can also be referred to as regex or regexp. The basic method for applying a regular expression is to use of binding operators =~ (Regex Operator) and !~ (Negated Regex Operator). There are three types of regular expression operators in Perl:
- Match Regular Expression
- Substitute (Search and replace) Regular Expression
- Global Character Transliteration Regular Expression
1) Pattern Matching or Match Regular Expression: The match operator "m//" is used to match a string or a statement against a regular expression. The forward slash used in the operator ( m// ) acts as the delimiter and this delimiter can also be like m{}, m(), and m><, etc. The expression is written in between two forward slashes used in the operator.
Syntax: m/PATTERN/
Here, PATTERN is the Regular Expression to be searched in the string Let's see some examples illustrating the pattern matching: In the below examples, a string and regular expression is matched, on success it returns "match found" otherwise "match not found". Example 1:
#!/user/bin/perl
# Initializing a string
$a = "GeeksforGeeks";
# matching the string and
# a regular expression and returns
# match found or not
if ($a =~ m/for/)
{
print "Match Found\n";
}
else
{
print "Match Not Found\n";
}
Output:
Match Found
Example 2:
#!/user/bin/perl
# Initialising an string
$a = "GeeksforGeeks";
# matching the string and
# a regular expression and returns
# match found or not
if ($a =~ m:abc:)
{
print "Match Found\n";
}
else
{
print "Match Not Found\n";
}
Output:
Match Not Found
Here, in the above code a different delimiter ':' is used instead of '/', this shows that it is not necessary to use '/' as a delimiter. 2) Substitute (Search and replace) Regular Expression: The substitute operator "s///" is used to search a specific word and then replace it with a given regular expression. The forward slash used in the operator ( s/// ) acts as the delimiter.
Syntax: s/PATTERN/REPLACEMENT/;
Here PATTERN is the regular expression which is to be replaced with REPLACEMENT regular expression. Let's see some examples illustrating the substitute regular expression: In the below examples, a PATTERN word is searched first then it is replaced with the REPLACEMENT word. Example-1:
#/user/bin/perl
# Initialising a string
$string = "GeeksforGeeks is a computer science portal.";
# Calling the substitute regular expression
$string =~ s/GeeksforGeeks/gfg/;
$string =~ s/computer science/cs/;
# Printing the substituted string
print "$string\n";
Output:
gfg is a cs portal.
Example-2:
#/user/bin/perl
# Initialising a string
$string = "10001";
# Calling the substitution regular expression
$string =~ s/000/999/;
# Printing the substituted string
print "$string\n";
Output:
19991
3) Global Character Transliteration regular expression: The translation or transliteration operator "tr///" or "y///" is used to replace all the occurrences of a character with a given single character. The forward slash used in the operator ( tr/// and y/// ) acts as the delimiter.
Syntax: tr/SEARCHLIST/REPLACEMENTLIST/ y/SEARCHLIST/REPLACEMENTLIST/
Here SEARCHLIST is the character whose all the occurrences are going to be replaced with the character in REPLACEMENTLIST. Let's see some examples illustrating the translation regular expression: In the below examples, all occurrences of "G" are replaced with "g" with two different operators "tr///" and "y///". Example 1:
#/user/bin/perl
# Initialising a string
$string = 'GeeksforGeeks';
# Calling the tr/// operator
$string =~ tr/G/g/;
# Printing the replaced string
print "$string\n";
Output:
geeksforgeeks
Example 2:
#/user/bin/perl
# Initialising a string
$string = 'GeeksforGeeks';
# Calling the y/// operator
$string =~ y/G/g/;
# Printing the replaced string
print "$string\n";
Output:
geeksforgeeks