in reply to Silly regex with match extrapolation

Can the multiple matches overlap? How many match strings do you expect? How long do you expect the match strings to be? Are the match strings simple strings, or do they involve regex components? How long do you expect the target string to be? Do you expect there to be multiple matches for an individual match string?


Perl reduces RSI - it saves typing
  • Comment on Re: Silly regex with match extrapolation

Replies are listed 'Best First'.
Re^2: Silly regex with match extrapolation
by elanghe (Novice) on Oct 14, 2008 at 00:43 UTC
    The patterns to match on are simple strings. The being checked will contain the html and txt portions of email messages. There may be multiple matches per scalar being tested. So if I'm testing $scalar for /bob|jan|mike/ $scalar might have any combination of all three and even multiple instances of all three. But, I only need to know about the fist instance of each. Essentially, does $scalar match for each bob jan and mike. Hope that makes sense. Thank you for the help.

      Take a look at Regexp::List. Consider:

      use strict; use warnings; use Regexp::List; my @words = qw!bob fred frederic ann anne annie!; my $target = <<DATA; This list contains Bob, Frederic, Fred, Robert, Annette and Ann. And this list conatins Fred and Frederic. DATA my $match = Regexp::List->new (modifiers => 'i')->list2re (@words); my @matches = $target =~ /($match)/g; print "@matches";

      Prints:

      Bob Frederic Fred Anne Ann Fred Frederic

      Perl reduces RSI - it saves typing
        That's perfect! Thank you so much!
      Probably not very efficient, but just count them:
      use warnings; use strict; my @names = qw(bob jan mike); while (<DATA>) { my $cnt = 0; for my $name (@names) { $cnt++ if /\b$name\b/i; } print if $cnt == 3; } __DATA__ Mike went to the store with Jan. Jan, Bob and Mike ate lunch. In January, we bobbed for apples and sang into a mike.

      Prints:

      Jan, Bob and Mike ate lunch.