Perl matching operator
Last Updated :
07 May, 2019
m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.
To print this matched pattern and the remaining string, m operator provides various operators which include $, which contains whatever the last grouping match matched.
$& – contains the entire matched string
$` – contains everything before the matched string
$’ – contains everything after the matched string
Syntax: m/String/
Return:
0 on failure and 1 on success
Example 1:
$string = "Geeks for geeks is the best" ;
$string =~ m/or g/;
print "Before: $`\n" ;
print "Matched: $&\n" ;
print "After: $'\n" ;
|
Output:
Before: Geeks f
Matched: or g
After: eeks is the best
Example 2:
$string = "Welcome to GeeksForGeeks" ;
$string =~ m/to Ge/;
print "Before: $`\n" ;
print "Matched: $&\n" ;
print "After: $'\n" ;
|
Output:
Before: Welcome
Matched: to Ge
After: eksForGeeks
Share your thoughts in the comments
Please Login to comment...