-2

The text has many occurrences of pattern; while doing regex find-and-replace, I wanna jump over certain segments of the text, and replace pattern in the remaining part. Example, in the code:

#!/usr/bin/env perl
use strict;
use warnings;

#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__
START xx old xx END     --> within boundaries,  should NOT replace
START xx old
      xx old xx END     --> within boundaries,  should NOT replace
// xx old               --> within comment,     should NOT replace
xx // xx old            --> within comment,     should NOT replace
. old old xx            --> following a point,  should NOT replace
                            first one, just replace second one
xx .
  old
  old xx                --> following a point,  should NOT replace first
                            one, just replace second one.
xx old xx               --> other scenarioes,   should REPLACE

EDIT 16.2.22(updated 16.2.23) The criteria for replace/no replace is as follows: (1) START and END may be on one line or span multiple lines, all patterns within this range should NOT be replaced;

(2) . and pattern may or may not have spaces, tabs, newlines between them, the first occurrence of pattern after . should be replaced;

(3) comments will always just be one line starting with //; do not consider /* ... */ style of comments for the time being.

(4) // may or may not be the first character of a line; so it is with ..

Anything between START_FLAG and END_FLAG, or anything within a comment, should be ignored; and, if the pattern follows a ".", it should also be ignored. patterns in the remaining part of the text should be replaced by new stuff. I tried to use s/START.*?END|\/\/.*?\n|.\s*\w+|\w+//g stuff, but just cannot reach a solution.

This seems little bit convoluted for me; any help? Thx in advance :-)

katyusza
  • 325
  • 2
  • 12
  • Thanks to the suggestions of someone who helped answer this question, I realized my question has many ambiguities which preclude a complete coverage of all cases that I intended to cover. I'll just see if I can put a new post with a revised way of expressing my question. – katyusza Feb 22 '16 at 06:45
  • Your comment in the code and EDIT is conflicting. In the code you are saying between START and END, with in comments, should NOT replace, and in your EDIT, you say all the pattern within START END range and first occurrence after . should be replaced. – SwiftMango Feb 22 '16 at 19:46
  • @texasbruce Thanks for your careful examination of the post, dear friend. I already updated the post for consistency. Actually this post is not a "good" question; I already posted a new question at [click_this_link](http://stackoverflow.com/questions/35547683/how-to-ignore-parts-of-the-text-and-do-search-and-replace-in-the-remaining-part), and it's been solved. Thanks, again :-) – katyusza Feb 23 '16 at 03:43

1 Answers1

-1

Use normal if statement would do:

while (<DATA>) {
  next if (m/^START/ && m/END$/ ) ||  m/^\/\// || m/^\./;
  s/old/new/gs;
  print;
}

NOTE Above is the answer prior to the OP's edit 16.2.22.

SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • This one doesn't seem to solve the problem buddy~ – katyusza Feb 22 '16 at 06:29
  • @katyusza Does not seem to? Care to elaborate? – SwiftMango Feb 22 '16 at 16:58
  • @texasbruce After squeezing more information out of the OP, it quickly became clear that the question was not as simple as it appeared, and that a proper solution would require a full-blown parser. Since I didn't feel like taking on that amount of work just to answer a question, I deleted my simplistic answer. I suggest you do the same. – Matt Jacob Feb 22 '16 at 19:00
  • @MattJacob You are right. He updated the question. This is the answer prior the update. Will not delete it though. – SwiftMango Feb 22 '16 at 19:37
  • Buddies, the question was solved at a new post, link is: http://stackoverflow.com/questions/35547683/how-to-ignore-parts-of-the-text-and-do-search-and-replace-in-the-remaining-part thanks again ;-) – katyusza Feb 23 '16 at 03:49