in reply to mysteries of regex substring matching
I think unpack is better for this as haukex has suggested, but here's a pure-regex solution:
The trick is to have an unambiguous look-around anchor.Win8 Strawberry 5.8.9.5 (32) Fri 01/15/2021 18:04:55 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -MData::Dump=dd my $s = q(AAAAbbbbCCCCddddXeeeeeeeeeeX); my @caps = $s =~ m{ (?<! \A .{16}) \G .{4} | .* }xmsg; dd \@caps; ^Z ["AAAA", "bbbb", "CCCC", "dddd", "XeeeeeeeeeeX", ""]
Update: Another variation:
Win8 Strawberry 5.8.9.5 (32) Fri 01/15/2021 18:19:20 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings -MData::Dump=dd my $s = q(AAAAbbbbCCCCddddXeeeeeeeeeeX); my $n = 4; my $m = 3; my @caps = $s =~ m{ (?<! \A (?: .{$n}){$m}) \G .{$n} | .* }xmsg; dd \@caps; ^Z ["AAAA", "bbbb", "CCCC", "ddddXeeeeeeeeeeX", ""]
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: mysteries of regex substring matching (explanation)
by LanX (Saint) on Jan 16, 2021 at 11:43 UTC |