|
|
|
Your skill will accomplish what the force of many cannot |
|
| PerlMonks |
Re: A regex that only matches at offset that are multiples of a given N?by smls (Beadle) |
| on Feb 13, 2013 at 23:35 UTC ( #1018660=note: print w/ replies, xml ) | Need Help?? |
|
Alright, here is a revised version of johngg's solution, that prevents the redundant double matching, while at the same time keeping the matching logic self-contained within the regex. Instead of moving pos() forward manually (like I suggested in the discussion thread for johngg's solution), it lets the regex engine do this implicitly by having it gobble up $n characters (if available) after matching the zero-width look-ahead that contains the capture group:
Output:
Note that if length("fred$1") > $n, it will actually start looking for the next "fred" while still whithin the part matched by $1. If this must be avoided, I guess manual pos()-incrementing is still the best bet.
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||