in reply to RegExp to Search All Array Members?

You were SO close.
my $regexStr="^static\s+\w+\s+(" # Thanks moritz! . (join "|",map quotemeta,@names) . ")\W.*)"; while(<STDIN>) { chomp; if(/$regexStr/) { # rest is the same as your "optimized" # attempt

Mike

Edit: added map quotemeta as suggested by moritz

Replies are listed 'Best First'.
Re^2: RegExp to Search All Array Members?
by moritz (Cardinal) on Jun 19, 2007 at 10:26 UTC
Re^2: RegExp to Search All Array Members?
by ff (Hermit) on Jun 19, 2007 at 13:19 UTC
    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(...);

      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.
Re^2: RegExp to Search All Array Members?
by johngg (Canon) on Jun 19, 2007 at 13:33 UTC
    An alternative to using join and string concatenation when making the pattern would be to change the default list separator from a space to the pipe symbol and use interpolation with the array or list.

    $ perl -Mstrict -Mwarnings -le ' > my @arr = qw{abc d.ef gh?i}; > my $patt; > { > local $" = q{|}; > $patt = qq{xyz(@{ [ map quotemeta, @arr ] })123}; > } > print $patt;' xyz(abc|d\.ef|gh\?i)123 $

    You can also use this method with qr{...} which behaves like double quotes.

    Cheers,

    JohnGG