Say, a text file have many $start-$end
pairs, and within each pair there are some text. I want Perl to find-and-replace all $pattern
s with the
Looking at your 'source' I would suggest the trick here is to set $/
- the record separator.
If you set it to a single space, you can iterate word by word. And then use a range operator to determine if you're within delimiters.
Example:
#!/usr/bin/env perl
use strict;
use warnings;
local $/ = ' ';
while ( ) {
if ( m/START/ .. /END/ ) {
s/bingo/okyes/g;
}
print;
}
__DATA__
xx START xx bingo xx bingo xx END xx bingo xx START xx bingo xx END bingo
This prints:
xx START xx okyes xx okyes xx END xx bingo xx START xx okyes xx END bingo
You could probably accomplish this with a single regex. I'm going to suggest that you don't because it'll quite complicated and hard to understand later.