The stupid question is the question not asked | |
PerlMonks |
Can I determine index point of failure in string during regex match attemptby tj_thompson (Monk) |
on May 06, 2014 at 21:44 UTC ( [id://1085239]=perlquestion: print w/replies, xml ) | Need Help?? |
tj_thompson has asked for the wisdom of the Perl Monks concerning the following question: 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:
Back to
Seekers of Perl Wisdom
|
|