Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Storing pattern matches

by ww (Archbishop)
on Jan 04, 2014 at 02:23 UTC ( [id://1069227]=note: print w/replies, xml ) Need Help??


in reply to Storing pattern matches

Not positive I've understood your question correctly, but are you looking for something like this?
#!/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"; } }
Execution:
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.

If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1069227]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-19 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found