Here's a subroutine that solves the problem if you want
an *exact* match - no regex supported. The solution can
be extended to regexes, but you have to be careful,
iterating with m//g over a line to "find all matches"
will fail, as m//g will never find overlapping matches.
sub multi_match {
my ($large, $small) = @_;
my @large = split /\n/ => $large;
my @small = split /\n/ => $small;
my $first = $small [0];
foreach my $i (0 .. @large - @small) {
my $pos = 0;
LOOP:
while ((my $new_pos = index ($large [$i], $first, $pos)) != -1
+) {
$pos = $new_pos + 1;
foreach my $j (1 .. $#small) {
next LOOP unless
length $large [$j] >= $new_pos + length $small [$
+j] &&
$small [$j] eq substr $large [$i + $j], $new_pos,
length $small [$j];
}
return ($i, $new_pos);
}
}
return;
}
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, 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, 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.
|
|