While doing regex find-and-replace in text file, I wanna jump over & ignore certain segments of the text. That is, certain parts of the text should be excluded from the search, and only do search & replace in the remaining parts. The criteria is:
(1) anything between START
and END
should be excluded from the search & replace.
START
may or may not be at the start of a line;
END
may or may not be at the end of a line;
one pair of START
& END
may span multiple lines;
(2) anything wihtin inline comment //
should be ignored;
//
may or may not be at the start of line;
(3) the first word after .
should be ignored;
.
may or may not be at the start of a line;
the word may immediately follow .
or with spaces, newlines, tabs splitting them.
Example code:
#!/usr/bin/env perl
use strict;
use warnings;
$/ = undef;
#iterate the DATA filehandle
while (<DATA>) {
# This one replaces ALL occurrences of pattern.
s/old/new/gs;
# How do I skip the unwanted segments and do the replace?
#print all
print;
}
##inlined data filehandle for testing.
__DATA__
xx START xx old xx END xx --> ignore
xx old xx --> REPLACE !
START xx old --> ignore
xx old xx END --> ignore
xx old xx --> REPLACE !
// xx old --> ignore
xx // xx old --> ignore
xx . old old xx --> ignore first one, replace second one
.
old --> ignore
(old) xx --> REPLACE !
xx old xx --> REPLACE !
Expected output is:
xx START xx old xx END xx --> ignore
xx new xx --> REPLACE !
START xx old --> ignore
xx old xx END --> ignore
xx new xx --> REPLACE !
// xx old --> ignore
xx // xx old --> ignore
xx . old new xx --> ignore first one, replace second one
.
old --> ignore
(new) xx --> REPLACE !
xx new xx --> REPLACE !
Can anyone help me with the regex here? I posted a similar question couple of hours ago, but that post was full of ambiguities and precludes a clear answer. Hopefully this post may be a "good" & "clear" question.