Getting multiple matches within a string using regex in Perl -
after having read this similar question , having tried code several times, keep on getting same undesired output.
let's assume string i'm searching "i saw wilma yesterday". regex should capture each word followed 'a' , optional 5 following characters or spaces.
the code wrote following:
$_ = "i saw wilma yesterday"; if (@m = /(\w+)a(.{5,})?/g){ print "found " . @m . " matches\n"; foreach(@m){ print "\t\"$_\"\n"; } }
however, kept on getting following output:
found 2 matches "s" "w wilma yesterday"
while expected following one:
found 3 matches: "saw wil" "wilma yest" "yesterday"
until found out return values inside @m
$1
, $2
, can notice.
now, since /g
flag on, , don't think problem regex, how desired output?
you can try pattern allows overlapped results:
(?=\b(\w+a.{1,5}))
or
(?=(?i)\b([a-z]+a.{0,5}))
example:
use strict; $str = "i saw wilma yesterday"; @matches = ($str =~ /(?=\b([a-z]+a.{0,5}))/gi); print join("\n", @matches),"\n";
more explanations:
you can't have overlapped results regex since when character "eaten" regex engine can't eaten second time. trick avoid constraint, use lookahead (that tool checks, not matches) can run through string several times, , put capturing group inside.
for example of behaviour, can try example code without word boundary (\b
) see result.
Comments
Post a Comment