in reply to A regex that only matches at offset that are multiples of a given N?
Similar to tobyink's solution but doing a global match in case there are multiple "fred"s on your intervals. It seems to work except that it matches twice at each point, probably for similar reasons as explored here.
]$ perl -Mstrict -Mwarnings -E ' $_ = q{abcdefredfghfredijklmnopfredqrs}; for my $n ( 4, 5 ) { say qq{\$n = $n}; say qq{Matched $1 at position @{ [ pos( $_ ) ] }} while m{\G(?:.{$n})*?(?=(fred.*))}g; }' $n = 4 Matched fredijklmnopfredqrs at position 12 Matched fredijklmnopfredqrs at position 12 Matched fredqrs at position 24 Matched fredqrs at position 24 $n = 5 Matched fredfghfredijklmnopfredqrs at position 5 Matched fredfghfredijklmnopfredqrs at position 5 $
I hope this is useful.
Cheers,
JohnGG
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: A regex that only matches at offset that are multiples of a given N? (Update:almost perfect!)
by BrowserUk (Patriarch) on Feb 13, 2013 at 16:39 UTC | |
by smls (Friar) on Feb 13, 2013 at 17:52 UTC | |
by BrowserUk (Patriarch) on Feb 13, 2013 at 18:45 UTC | |
by smls (Friar) on Feb 13, 2013 at 19:29 UTC | |
by BrowserUk (Patriarch) on Feb 13, 2013 at 20:11 UTC | |
|
In Section
Seekers of Perl Wisdom