in reply to Re: RegExp to Search All Array Members?
in thread RegExp to Search All Array Members?
Here's the snippet again with some tweaks to your example. Note, whenever I store a reference in a scalar, I like to suffix the identifier with _Xr, where X stands for the kind of thing that's supposed to be referenced (in this case r for "regex") and the closing r indicates that the scalar is supposed to hold a reference.
#!/usr/local/bin/perl -w use strict; my @names = ("John?", "Paul", "George", "Rin.go"); my $regexStr = "^static\\s+\\w+\\s+(" # Thanks moritz! . (join "|",map quotemeta,@names) . ")\\W.*"; print "$regexStr\n\n"; my $regexStr_rr = qr{$regexStr}i; # or "cloister" the 'i' in $regexStr while(<DATA>) { chomp; my ( $hit ) = $_ =~ /$regexStr_rr/; if ($hit) { print "Beatle method \"$hit\" found on this line: $_\n"; } else { print "No Beatle method found on this line: $_\n"; } } __DATA__ static int lars(...); static bool george(...); static int thom(...);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: RegExp to Search All Array Members?
by Aceflex (Initiate) on Jan 03, 2012 at 23:33 UTC | |
by Anonymous Monk on Aug 16, 2012 at 20:42 UTC |