in reply to Re: Silly regex with match extrapolation
in thread Silly regex with match extrapolation

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.
  • Comment on Re^2: Silly regex with match extrapolation

Replies are listed 'Best First'.
Re^3: Silly regex with match extrapolation
by GrandFather (Saint) on Oct 14, 2008 at 01:13 UTC

    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!
Re^3: Silly regex with match extrapolation
by toolic (Bishop) on Oct 14, 2008 at 01:29 UTC
    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.