ant replaceregex replace multiline

允我心安 提交于 2019-12-07 02:13:41

问题


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.



来源:https://stackoverflow.com/questions/10256840/ant-replaceregex-replace-multiline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!