blackicecube
blackicecube

Reputation: 1423

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

I have a classpath, something like:

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

Now I would like to clean up this classpath and throw away somethings which I don't want anymore. Hence in this case the classpath should look something like

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

How can I do that with a regular expression? I want to do it with an ant-Script, e.g.

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

How do I need to adapt the regexp? Thanks a lot!

Upvotes: 1

Views: 735

Answers (2)

Richard Szalay
Richard Szalay

Reputation: 84734

I think you would need to phrase it as (validstuffbefore)skipstuff(validstuffafter) and then use a replace string of \1\2

So (possibly):

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

And replace with:

\1\2\3

You may have issues with multiple matches, but the global flag should resolve that.

Good luck!

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

If all you want to do is get rid of a contiguous string in the middle, it should just be:

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

Upvotes: 1

Related Questions