in reply to Re^2: 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.

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?
  • Comment on Re^3: Need a Regular Expression that tests for words in different order and captures the values found.
  • Download Code

Replies are listed 'Best First'.
Re^4: Need a Regular Expression that tests for words in different order and captures the values found.
by AnomalousMonk (Archbishop) on Jan 15, 2010 at 18:04 UTC
    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;

Re^4: Need a Regular Expression that tests for words in different order and captures the values found.
by ikegami (Patriarch) on Jan 15, 2010 at 18:04 UTC