Ant replaceregex replace multiline

I need to replace a multi-line line in a file, for example:

startString bla bla bla ... endString 

ant replaceregex. Ant code:

  <copy file="${file}" tofile="${newFile}" overwrite="true"> <filterchain> <replaceregex pattern="startString(.+)endString" replace="zzz" flags="gmi" byline="true"/> </filterchain> </copy> 

If the text to replace is a single line, everything works correctly, but when the text is multi-line, replaceregex does not work. What should I fix in my code?

+6
source share
1 answer

There are a few changes you need to make. There are a couple of settings that you suggested that each input line be considered a separate input line, which is a byline attribute and m flag. In the following, I deleted those, and also added the s flag, which processes the input file with a single line of input:

 <replaceregex pattern="startString(.+?)endString" replace="zzz" flags="gis" byline="false"/> 

Also pay attention to the addition ? into regex, which makes the template indecent if you have multiple matches that you want to match.

Cm

Ant ReplaceRegExp documentation for more details.

+17
source

Source: https://habr.com/ru/post/913770/


All Articles