Hello again Monks!

In addition to my long question below, I've come up with a shorter more theoretical question that should be easier to jump into.

Given a string $s, and a regex $r anchored at both front and end of the string with '^' and '$', is it possible to write a function could_match( $r, $s ) that will determine whether $r could match if additional characters were appended to $s?

One requirement: the entirety of $s must be matched, not a sub string of $s. $r could always match with additional characters if it was not anchored.

It seems this should be easy enough to answer if you can answer the following question: How much of string $s was consumed in the failed match attempt against $r? If the entire string was consumed and $r remained unmatched, then it would seem $r could match if characters were appended to $s. On the other hand, if only part of $s was consumed, then adding additional characters would not help and $r could not be matched.

Perhaps consumed is poor verbiage. You could also look at this as the index point of failure during the match attempt.

Examples:

# $s does not match, but '12' would be consumed in the # match attempt. In this case, all of $s is consumed # so $s should be able to match if additional characters # were added $s = '12' $r = qr/^123$/; could_match( $r, $s ) # returns TRUE # $s does not match and none of $s would be consumed in the # attempt as the '1' in the regex could not match. Since # $s was not consumed in it's entirety in the match attmempt # there are no characters that could be added to $s to allow # it to match $r $s = '23' $r = qr/^123$/; could_match( $r, $s ) # returns FALSE # $r in this case is not anchored, so it could always match # if more characters were added. However, since none of $s # was used in the match attempt, the entirety of $s could # not match even if more characters were added. $s = '123'; $r = qr/456/; could_match( $r, $s ) # returns FALSE

In reply to Can I determine index point of failure in string during regex match attempt by tj_thompson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.