Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,
This might be a very round about question to pose but the tag line of PERL monks says stupid most question is question not asked!! I have a code where I am matching lines in if else block. If the line matches to the pattern in the if block it performs some task. Same is for elsif blocks too. Finally if the line does not match to any of these it sends it to else block printing it as error. I want to know exactly where the pattern did not match the given line in the loop. Is there any efficient and less costly way to find this?

if($a=~/ABC\s+XYZ/){ #do something }elsif($a=~/PQR\s+LMN/){ #do something }else{ print "Error\n"; #Want to know where exactly did the regex in above checks failed OR ne +ed to tell the user that your query failed at this particular place. }

Replies are listed 'Best First'.
Re: How to find where regex did not match across the line
by moritz (Cardinal) on Mar 12, 2012 at 13:47 UTC
    I want to know exactly where the pattern did not match the given line in the loop

    well, everywhere.

    Or to phrase it differently, the regex didn't match anywhere, so every position in the string is a position where the match failed.

    So, the question is, what do you want to tell the user? That the line contained ABC but not a whitespace after it? then you need to run a second regex. Or you could presumably match a regex that always matches, and then use that to see how far you got, but it's not so easy.

    Update: One can do something like

    use strict; use warnings; $_ = 'stuffABC Xmorestuff'; if (/((A(?:B(?:C(?:\s+(?:X(?:Y(?:Z)?)?)?)?)?)?))/) { print "Searched for 'ABC\\s+XYZ' but only found $1"; }

    But that won't give good output in the most general case. Probably the most intuitive output is searching for the longest partial match:

    use strict; use warnings; $_ = 'stuAffABC XmoresABtuff'; my $longest = ''; while (/((A(?:B(?:C(?:\s+(?:X(?:Y(?:Z)?)?)?)?)?)?))/g) { $longest = $1 if length($1) > length($longest); } print "Searched for 'ABC\\s+XYZ' but only found '$longest'\n"; __END__ Searched for 'ABC\s+XYZ' but only found 'ABC X'

    Note that Perl 6 has a special regex feature for constructing such regexes (<* regex that can match partially>, though nobody has implemented that yet).

    Second update: I realized that it might help to explain why there isn't a good way to determine why a regex failed to match in the general case.

    One must understand that a successful regex match typically is typically preceeded by quite a number of "small" failures. For example if you do a match like 'abc' =~ /.*c/, then a naive regex engine would first try to match .* against the entire string, then determines that the 'c' can't match, then backtrack, that is make .* one less character, and then the 'c' can match too. (Perl actually keeps track of the length of the regex that remains, so it's a bit smarter, but there are other cases where it can't be smart).

    So if the regex .*c fails to match a string, it has tried every possible starting position within that string, and all the attempts have been unsuccessful. Which attempt should be reported as "the" unsuccessful attempt? There's no good answer to that question in the general case.

    Also a regex engine would need to keep around lots of data in memory to report why stuff failed, which isn't a good idea for successful matches.

    Note that if you run a program with perl -Mre=debug yourprogram.pl you'll get some information why a regex match failed, but it's not a format that would make it useful to a normal user (who isn't a programmer).

      Hello,
      Thanks for elaborating. Gives me a different dimension to code!

Re: How to find where regex did not match across the line
by LanX (Saint) on Mar 12, 2012 at 16:46 UTC
    You could execute code after each sub-regex that matters and matched using the (?{...}) construct.

    But be careful to avoid code regex-optimization,

    DB<6> "ab" =~ /(a)(?{print "match1"}).{0}(b)(?{print "match2"})/ match1match2 DB<7> "axb" =~ /(a)(?{print "match1"}).{0}(b)(?{print "match2"})/ match1

    see perlretut for details for why I try to avoid optimization with a attempted match of a ("useless") zero length dot .{0}

    Cheers Rolf

Re: How to find where regex did not match across the line
by ww (Archbishop) on Mar 12, 2012 at 15:27 UTC
    A possibly useful addendum to the excellent guidance above( if you were to break up your regex to find a series of partial matches): perlvar and seek down for '$`', the postmatch variable.
    The string following whatever was matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK).
    ...

    The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. To avoid this penalty, you can extract the same substring by using @-. Starting with Perl 5.10, you can use the

    match flag and the ${^POSTMATCH} variable to do the same thing for particular match operations.

    This variable is read-only and dynamically-scoped.

    See also ${^POSTMATCH} for a way to avoid the performace penalty.

      The word "match" being in the name "POSTMATCH" was not an accident. It is completely useless for a case of "not matching".

      - tye        

        The conditional, "if you were to break up your regex to find a series of partial matches," was not an accident either.

        Were OP to use postmatch to find the last partial that succeeded, he would have at least an initial indication of where the failure occured.

Re: How to find where regex did not match across the line
by Marshall (Canon) on Mar 12, 2012 at 16:20 UTC
    Can you give some example lines? And example output? The general parsing problem can be quite difficult, but maybe there are some application specific things that can be used to simplify the problem.