I need replace multiline string in file, like this:
startString
bla bla bla
...
endString
by 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 text for replace is Single line - all works correct, but when text is multiline - replaceregex doesn't work. What I should fix in my code?
There are a couple of changes you need to do. There are a couple of settings you had suggesting that each line of input should be considered a separate line of input which are the
byline
attribute and them
flag. In the following I have removed those and also added thes
flag which treats the input file a single line of input:Also note the addition of the
?
in the regex, this makes the wildcard non greedy in case you have multiple occurrences you want to match.See
The ant ReplaceRegExp documentation for more.