ant replaceregex replace multiline

2019-04-29 00:30发布

问题:

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?

回答1:

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 the m flag. In the following I have removed those and also added the s flag which treats the input file a single line of input:

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

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.



标签: ant