After having read this similar question and having tried my code several times, I keep on getting the same undesired output.
Let's assume the string I'm searching is "I saw wilma yesterday". The regex should capture each word followed by an 'a' and its optional 5 following characters or spaces.
The code I wrote is the following:
$_ = "I saw wilma yesterday";
if (@m = /(\w+)a(.{5,})?/g){
print "found " . @m . " matches\n";
foreach(@m){
print "\t\"$_\"\n";
}
}
However, I kept on getting the following output:
found 2 matches
"s"
"w wilma yesterday"
while I expected to get the following one:
found 3 matches:
"saw wil"
"wilma yest"
"yesterday"
until I found out that the return values inside @m
were $1
and $2
, as you can notice.
Now, since the /g
flag is on, and I don't think the problem is about the regex, how could I get the desired output?