How to perform search-and-replace within given $start-$end ranges?

后端 未结 4 772
星月不相逢
星月不相逢 2021-01-15 09:54

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 $patterns with the

4条回答
  •  走了就别回头了
    2021-01-15 10:29

    Eventually used following code to accomplish what I intended:

    $_ = "xx START xx bingo xx bingo xx END xx bingo xx START xx bingo xx END bingo";
    print;
    print "\n";
    $_ =~ s/START.*?END/($s=$&) =~ s,bingo,okyes,g; $s/ge;
    print;
    

    This is a one-regex solution, using embedded expression in s///g regex, and nested s///g regexes.

    Sorry for this late post, but I deeply appreciate the replies by @Sobrique, @Borodin and @choroba, which are enlightening and helpful.

提交回复
热议问题