in reply to Re: RegExp to Search All Array Members?
in thread RegExp to Search All Array Members?

Having just read the section in Damian's Object Oriented Perl book about the regular use of qr, this looked like a good chance to try it out. That is, with qr, you are creating a reference to a regular expression which you can store in a scalar variable. When that scalar variable is used in a regex later on, the complilation work to prepare the referenced regex doesn't have to be repeated each time through your loop, avoiding a good amount of regex overhead.

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

    I have tried to get this working, but have been unsuccessful. I am a noob to programming and perl so patience would be appreciated.

    The search NEVER matches. I made sure the case is exactly the same in the doc and still no luck

    Here is the code

    #!/usr/bin/perl -w use strict; use warnings; open(FH, "Wachterpdf2txt.txt") or die "We have a problem: $!"; my @invoiceSearch = ("phone", "hunt", "dial", "tone", "static", "18d", + "system", "voice", "numbers", "voicemail", "MLX201", "programming", +"extension", "processor", "block", "mls", "programmed", "rollover", " +extension", "partner", "crosstalk", "merlin", "ringing"); my $regexStr="^static\\s+\\w+\\s+(" . (join "|",map quotemeta,@invoice +Search) . ")\\W.*"; print "$regexStr\n\n"; my $regexStr_rr = qr{$regexStr}i; # or "cloister" the 'i' in $regexStr while(<FH>) { 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"; } }

    What I can't seem to find anywhere is what the ^static statement means in the Regex variable. I am pulling in a text document that was converted from .pdf I have stripped out all spaces and most punctuation. The text document looks fine to me.

    Any help would be appreciated!

      A bit late, but...

      ^ is a special character in regex that makes it find the match at the beginning of the string. So with '^static', it will find any string that begins with 'static'.

      So, it would match:
      static bool fred(...)

      But it wouldn't match
      stat bool fred(...)

      since static isn't at the very beginning of it, stat is.