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:
perl
#!/user/bin/perl
$a = "GeeksforGeeks";
if ( $a =~ m/ for /)
{
print "Match Found\n";
}
else
{
print "Match Not Found\n";
}
|
Output:
Match Found
Example 2:
perl
#!/user/bin/perl
$a = "GeeksforGeeks";
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:
perl
$string = "GeeksforGeeks is a computer science portal.";
$string =~ s/GeeksforGeeks/gfg/;
$string =~ s/computer science/cs/;
print " $string \n";
|
Output:
gfg is a cs portal.
Example-2:
perl
$string = "10001";
$string =~ s/000/999/;
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:
perl
$string = 'GeeksforGeeks' ;
$string =~ tr /G/g/;
print " $string \n";
|
Output:
geeksforgeeks
Example 2:
perl
$string = 'GeeksforGeeks' ;
$string =~ y/G/g/;
print " $string \n";
|
Output:
geeksforgeeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!