in reply to Storing pattern matches
Execution:#!/usr/bin/perl use 5.016; use warnings; # 1069225 save match templates for reuse? my $regex = qr/(sdfsd|abc|def|hijk)/; say "\t regex is: $regex\n"; my @string = ('xyzalmnopdef', 'sdfsd', 'hijk lmn op', 'babcdefijk2345', 'xaybz3', ); for my $string(@string) { if ( $string =~ /($regex)/g ) { # /g only if you expect to # find more than one match per li +ne say "got a match in $string: $1"; }else{ say "\t No match found in $string"; } }
C:\>1069225.pl regex is: (?^u:(sdfsd|abc|def|hijk)) got a match in xyzalmnopdef: def got a match in sdfsd: sdfsd got a match in hijk lmn op: hijk got a match in babcdefijk2345: abc No match found in xaybz3 C:\>
At the worst, perhaps this offers some help.
Update/PS: Failed to explain the /g. Fixed with comment in code.
|
|---|