Alexey
Alexey

Reputation: 157

ant replaceregex replace multiline

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?

Upvotes: 6

Views: 10736

Answers (1)

krock
krock

Reputation: 29619

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.

Upvotes: 20

Related Questions