Regex to select last line in a multi-line string

蹲街弑〆低调 提交于 2020-01-03 17:47:25

问题


I have an ANT script which will have a property whose value could be one or more lines e.g. property

prop1=
A_12.1_REL_B121000_10_18_2011.1700
A_12.1_REL_B121001_10_25_2011.6059
A_12.1_REL_B121001_10_25_2011.2201
A_12.1_REL_B121001_10_25_2011.2014

Please see that all these lines end with a CRLF and end of file is also another CRLF. Now what I need to do is just select the last line using a regex. The number of lines could be less or more e.g

prop1=
    A_12.1_REL_B121000_10_18_2011.1700  

In the second case I need to select this single line . I have searched older posts, but could not find anything specific. Any pointers ?


回答1:


If you are using ant-contrib :

    <loadfile srcFile="input.prop" property="test"/>

    <propertyregex property="result"
                   input="${test}"
                   regexp="(.*$)"
                   select="\1"
    />

    <echo message="Result is : ${result}"/>

This will always print the last line of your input properties file :

[echo] Result is : A_12.1_REL_B121001_10_25_2011.2014



回答2:


This should do it...

/^.*\z/m

See it in action.

(assume the m is multi-line mode.)



来源:https://stackoverflow.com/questions/8178864/regex-to-select-last-line-in-a-multi-line-string

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