Marius Mailat
Marius Mailat

Reputation: 68

Regular expression for a path directory

I need a regular expression for extracting a JAR name and a version. The input values are in the below format:

C:\WORK\Modify file\test2\lib\messageclass-12.jar
C:\WORK\Modify file\test2\lib\utilities-396.jar
C:\WORK\Modify file\test2679\lib\castor-1.jar

output should be:

messageclass
12

utilities
396

castor
1

Any help is appreciated. Update

I am using this in a ant script to extract in 2 properties the values: messageclass and 12 as bellow:

<propertyregex override="yes" property="propA" input="@{file}" regexp="here should be the regex" replace="" global="true" />    

and @{file} is infact C:\WORK\Modify file\test2\lib\messageclass-12.jar

A sample example can be found on: http://ant-contrib.sourceforge.net/tasks/tasks/for.html

Final solution based on your suggestions is:

            <!-- calculate jar name -->
            <propertyregex override="yes" property="jarname" input="@{file}" regexp="-(\d*)\.jar" replace="" global="true"/>
            <propertyregex override="yes" property="jarname" input="${jarname}" regexp=".*\\(\S*)\\" replace="" global="true"/>
            <!-- calculate jar version -->
            <propertyregex override="yes" property="jarversion" input="@{file}" regexp=".*\\(\S*)-" replace="" global="true"/>
            <propertyregex override="yes" property="jarversion" input="${jarversion}" regexp=".jar" replace="" global="true"/>

Upvotes: 1

Views: 2113

Answers (2)

Alptugay
Alptugay

Reputation: 1686

.*\\(\S*)-(\d*)\.jar

This should do the job.

Upvotes: 1

detunized
detunized

Reputation: 15289

Try this:

/.*\\(.*)-(\d+)\.jar/

Capture 1 would have the name, and capture 2 the version number.

This example on Rubular.

Upvotes: 1

Related Questions