Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Two-dimensional match/regex?

by Anonymous Monk
on May 23, 2002 at 13:20 UTC ( [id://168758]=note: print w/replies, xml ) Need Help??


in reply to Two-dimensional match/regex?

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168758]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-09-07 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.