in reply to Zero-width look-ahead regexp question

Decomposing a regex into component parts makes conceptualization and maintenance easier.

perl -wMstrict -le "my $beginning = qr{ \A FOO }xms; my $not_ending = qr{ (?! (?: BAR | BAZ) \z ) }xms; my $ending = qr{ .* \z }xms; for my $string (@ARGV) { if ($string =~ m{ $beginning $not_ending ($ending) }xms) { print qq($string accepted ('$1' to right)) } else { print qq($string rejected) } } " FOOABC FOO_BAR FOOBA FOOBAZZZZ "FOOBAR BAZ" FOOBAR FOOBAZ BARBAZ FOOABC accepted ('ABC' to right) FOO_BAR accepted ('_BAR' to right) FOOBA accepted ('BA' to right) FOOBAZZZZ accepted ('BAZZZZ' to right) FOOBAR BAZ accepted ('BAR BAZ' to right) FOOBAR rejected FOOBAZ rejected BARBAZ rejected

To make your life simpler, avoid as the plague the use of capturing groups in the decomposed regexes (the  qr{ ... } regexes); only use them in the matching regexes (e.g., the  m{ ... }), where it's a lot easier to keep track of them.

Replies are listed 'Best First'.
Re^2: Zero-width look-ahead regexp question
by rovf (Priest) on Jul 02, 2008 at 06:54 UTC

    This approach to writing a regexp is a novel idea to me, but I like it a lot. I've spent already too much time deciphering my own complex regexpes I've written some time ago (despite my, probably too lazy, use of comments).

    -- 
    Ronald Fischer <ynnor@mm.st>