Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant replaceregex replace multiline

Tags:

ant

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?

like image 440
Alexey Avatar asked Apr 21 '12 06:04

Alexey


1 Answers

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.

like image 160
krock Avatar answered Dec 06 '22 05:12

krock