in reply to Re^2: Match, Capture and get position of multiple patterns in the same string
in thread Match, Capture and get position of multiple patterns in the same string
#!/usr/bin/perl use strict; my $string = "CATINTHEHATWITHABAT"; my $regex = '\wAT'; my @matches = (); while ($string =~ s/($regex)/lc($1)/e){ my $match = $1; my $length = length($&); my $pos = length($`); my $start = $pos + 1; my $end = $pos + $length; my $hitpos = "$start-$end"; push @matches, "$match found at $hitpos "; } print "$_\n" foreach @matches; print "$string\n";
I've used the e modifier (see perlretut) to evaluate the lower-case transliteration of the matched string.
Caveat: This becomes an infinite loop if you use the i modifier, since you will continuously overwrite the first occurrence of 'cat'. If need case insensitivity, perhaps you'd want my $regex = '[A-Z][aA][tT]|[a-z][aA]T|[a-z]At'; or equivalent for your real case.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Match, Capture and get position of multiple patterns in the same string
by richardwfrancis (Beadle) on Nov 16, 2009 at 06:56 UTC |