http://www.perlmonks.org?node_id=763892


in reply to Re: Regexp matching words, not doing what I expect
in thread Regexp matching words, not doing what I expect

Or without the extended regex features of 5.10 (basically moritz's approach, but with only one capture):
>perl -wMstrict -le "my $text = q{hello to 'all' this perl-monk's friends}; my $wchar = qr{ [A-Za-z'-] }xms; my $word = qr{ (?<! $wchar) $wchar+ }xms; while ($text =~ m{ (?= ($word \s+ $word)) }xmsg) { print $1 } " hello to to 'all' 'all' this this perl-monk's perl-monk's friends

Replies are listed 'Best First'.
Re^3: Regexp matching words, not doing what I expect
by JavaFan (Canon) on May 13, 2009 at 20:42 UTC
    The 5.10 features can easily be avoided: the (?-1) can be replaced by just copying the [A-Za-z'-] part, and the (*FAIL) can be replaced with (?!).