In Perl, Regular expressions are user-determined strings that we can use for pattern-matching in a text. We use these patterns to determine if the ‘target’ string has the characteristics defined in the pattern.
The following are the three operators used for pattern matching in Perl:
Modifier | Description |
i | This modifier makes the pattern matching case-sensitive. |
o | This modifier makes sure that the pattern is matched once at most. |
s | This modifier allows using "." to match a newline character. |
x | This modifier allows the use of whitespace in the pattern. |
g | This modifier makes sure all matches globally are found. |
cg | This modifier lets the search continue even if there is no global match |
Modifier | Description |
i | This modifier makes the pattern matching case-sensitive. |
o | This modifier makes sure that the pattern is only matched at most once. |
s | This modifier allows using "." to match a newline character. |
x | This modifier allows the use of whitespace in the pattern. |
g | This modifier makes sure all matches globally are found. |
e | This modifier evaluates the replacement text as Perl code and uses the evaluated result as the replacement text. |
Modifier | Description |
c | This modifier complements the searchlist. |
d | This modifier is used to delete characters that were found but not replaced |
s | This modifier is used to squash duplicate replaced characters |
The following coded examples show the various uses of regular expressions.
# match regular expression$str = "This is an Edpresso shot.";if ($str =~ /shot/) { #searches for a matchprint "Found a match.\n";} else {print "No match found.\n";}# substitute regular expression$str =~ s/Edpresso/Educative/; # substitute Edpresso with Educativeprint $str, "\n";# transliterate regular expression$str =~ tr/a/o/; # modify all "a" characters to "o"print $str, "\n";
Free Resources