![](/img/trans.png)
[英]How can I use Regular Expression to convert String to different substring?
[英]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.