in reply to Re: Need a Regular Expression that tests for words in different order and captures the values found.
in thread Need a Regular Expression that tests for words in different order and captures the values found.

You need to add \b or something in front of fred, barney and joe. You're not suppose to be matching alfred.
  • Comment on Re^2: Need a Regular Expression that tests for words in different order and captures the values found.

Replies are listed 'Best First'.
Re^3: Need a Regular Expression that tests for words in different order and captures the values found.
by greatwazzoo (Novice) on Jan 15, 2010 at 14:50 UTC
    Thanks,
    What would I do in case of hyphens? where, I had a line that contained:
    "pseudo-fred flintstone" and I wanted to skip it because this wasn't the real fred that I was keying on.
    $line =~ /(?=.*\bfred\s+(\w+)/ ; # would get "fred" and anything "-fred" # how would I avoid that?
      What would I do in case of hyphens?

      You need to figure out a boundary condition, then define it as a regex.

      For instance, in the case of  (?=.*\bfred\s+(\w+)) (note the closing parenthesis, missing in your reply), the boundary condition is the  \b word boundary assertion.
      It might be defined
          my $boundary = qr{ \b }xms;
      and used (without the //x modifier) (untested)
          (?=.*${boundary}fred\s+(\w+))

      Now just change the definition of  $boundary to fit your needs. For instance, if you wanted first names to follow anything that was not a word character and also not a hyphen, you might define (untested)
          my $boundary = qr{ (?<! [\w-]) }xms;