in reply to Regex failing

When debugging regular expressions (and the text they should match against), I find a good technique to chop off elements from the regex until it matches:

my $line = 'amss\products\76xx\wconnect\bthost\brew\statext\src\aeebte +xtag.c-36572;FILE.flf;//source/wconnect/bthost/brew/statext/rel/00.00 +.26/src/AEEBTExtAG.c - LABEL : 1'; print "Has ;/" if ($line =~ /;\//); print "Has ;\/[^\/]" if ($line =~ /;\/[^\/]/); print "Has ;\/[^\/](.*)\s-\s" if ($line =~ /;\/[^\/](.*)\s-\s/); (my $file_name) = $line =~ /;\/[^\/](.*)\s-\s/; print $file_name;

That way you can easily see in your program where the matching stops. You could also use re 'debug', or output messages when the regular expression engine backtracks, but I found it always easier to manually create the relevant steps to output where my match progresses.

Replies are listed 'Best First'.
Re^2: Regex failing
by Fletch (Bishop) on Feb 03, 2011 at 13:48 UTC

    Along similar lines, using the debugger (just run perl -de 0) is a good way to "prototype" your regexen. Set $_ to your target string and then just plug away building up x m{foo}, x m{fo(x|obar)}, ... until you get something that works.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.