当前位置: 动力学知识库 > 问答 > 编程问答 >

regex - Ant-Contrib propertyregex return all matches by appending chars

问题描述:

My ant build can receive any one of the following input (typical classpath);

D:\Source\Dir1

D:\Source\Dir1;

D:\Source\Dir1;D:\Source\Dir2

/Source/Dir1

/Source/Dir1:

/Source/Dir1:/Source/Dir2

And I would like to transform above entries as follows via RegEx;

-ID:\Source\Dir1

-ID:\Source\Dir1

-ID:\Source\Dir1 -ID:\Source\Dir2

-I/Source/Dir1

-I/Source/Dir1

-I/Source/Dir1 -I/Source/Dir2

So far I have following in my ANT;

<propertyregex property="Example1-Result" input="${example}" regexp="[^${path.separator}^$]+(?:)*" replace="-I\0 " global="true"/>

Which generates following :(

 -ID:\Source\Dir1

-ID:\Source\Dir1 ;

-ID:\Source\Dir1 ;-ID:\Source\Dir2

-I/Source/Dir1

-I/Source/Dir1 :

-I/Source/Dir1 :-I/Source/Dir2

Just want to get rid of the ; and : from the the result.

网友答案:

I would do it in two passes to make this clearer. The first statement is yours:

<propertyregex property="Example1-Result" input="${example}" 
            regexp="[^${path.separator}^$]+(?:)*" replace="-I\0 " global="true"/>

The second is a simple search and replace to remove any colons or semi-colons that remain:

<propertyregex property="Example1-Result" input="${Example1-Result}" 
                    regexp="[:;]" replace=" " global="true" override="true"/>

This approach assumes that there aren't semicolons or colons in normal file names, but that seems unlikely.

分享给朋友:
您可能感兴趣的文章:
随机阅读: