If $r is really just a plain, simple string of characters, it seems to me you're asking questions that can easily be answered by index. If $r is a regex of limitless complexity, it seems much more difficult, perhaps impossible, to answer the question "could there be a match if more characters were added to the right end of $s?"
c:\@Work\Perl>perl -wMstrict -le
"my $r = '123';
;;
for my $s ('', qw(1 12 123 1234 2 23 234)) {
my $left_same = 0 == index($r, $s);
my $all_same = $r eq $s;
printf qq{%-6s with more stuff could %smatch with '$r' \n},
qq{'$s'}, ($left_same and not $all_same) ? '' : 'NOT '
;
}
"
'' with more stuff could match with '123'
'1' with more stuff could match with '123'
'12' with more stuff could match with '123'
'123' with more stuff could NOT match with '123'
'1234' with more stuff could NOT match with '123'
'2' with more stuff could NOT match with '123'
'23' with more stuff could NOT match with '123'
'234' with more stuff could NOT match with '123'
Updates:
- Maybe the idea of Levenshtein distance (LD) could also be brought to bear. I.e., if the two strings (again, I'm assuming they're both just simple strings) are the same at the left, the LD gives an idea of how much one must change the shorter string to make it the same as the longer. (LD == 0 means both strings were exactly the same to begin with.)
-
OTOH, If you just want to answer the question "can $s with one or more characters added and anchored at the start of $r match?" (again assuming $s $r to be plain strings), the regex
$r =~ m{ \A \Q$s\E .+ }xms
would seem to do the trick (note reversal of $s and $r).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.