繁体   English   中英

如何从带有正则表达式的字符串中剥离子字符串?

[英]How can I strip a substring from a string with a regular expression?

我有一个类路径,类似于:

myproject/classes;myproject/lib/somecrab.zip;myproject/lib/somelib1.jar;myproject/lib/somelib2.jar;myproject/lib/somelib3.jar;

现在,我想清理这个类路径,并丢弃一些我不再想要的东西。 因此,在这种情况下,类路径应类似于

myproject/classes;myproject/lib/somelib1.jar;myproject/lib/somelib3.jar;

我该如何使用正则表达式呢? 我想用一个蚂蚁脚本,例如

<pathconvert property="new.classpath" pathsep=";">
    <path refid="old.classpath" />
    <chainedmapper>
        <regexpmapper from="(.*).jar" to="\1.jar" />
    </chainedmapper>
</pathconvert>

我该如何适应正则表达式? 非常感谢!

如果您只想去除中间的连续字符串,它应该是:

<regexpmapper from="(.*)myproject/lib/somecrab.zip;(.*)" to="\1\2" />

我认为您需要将其措辞为(validstuffbefore)skipstuff(validstuffafter),然后使用\\ 1 \\ 2的替换字符串

因此(可能):

^([\w/]+;)((?:[\w/]+\.jar;)*)[\w/]+\.zip;?((?:[\w/]+\.jar;)*)$

并替换为:

\1\2\3

您可能有多个匹配项的问题,但是使用全局标志可以解决该问题。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM