Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

quick regex question

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
0
0
EU
Hi,

Im trying to use a regex to get just the name of the directory path folder from a string. I can get the build part on its own using .*/ but is there a way to get just "distribution/maintance/java/source" with out the last forward slash.

distribution/maintance/java/source/build

I'm very new to regex and cant find any examples of this.

Thanks
Dexter
 
Hi Dexter,
I stick with what I know. I'd just substring the last character if you know you're always getting the forward slash on the end. If you want a better answer, can you share a code snippet? Thanks.
 
I know nothing about regex, but this should do the job.

Code:
String path = fullpath.substring(0, fullpath.lastIndexOf("/")-1);

Cheers,
Dian
 
Hi

Thanks for the replies. I'm actually using this in an ANT script that uses a regex tag. Normally i would use the substring if i was using java but im restricted to using the regex. i think i should be able to get around this by just using the full path where thats needed. not pretty but it should do the job.

thanks for the help
dexter
 
Showing the critical part of your build.xml could help to understand more clearly what you tried.
If you would have mentioned before, that we talk about ant, it would have prevented us from wild guessing in the wrong direction.

Replace /$ by nothing should be possible with ant though.

don't visit my homepage:
 
try includes="**"
(you can use excludes to filter out something that you do not want)
I think it is not about regex. ** is the syntax for ant
 
Hi,

Thanks for the help. here's exactly what im doing and sorry for not mentioning i was using ant, i presumed it would be just a regex issue.

a directory path for each module will be passed into an ant loop.

e.g. distribution/maintance/java/source/nameOfModule

there are no forward slashes at the beginning or the end.

What i wanted to do was to take in this string and chop off "/nameOfModule" and keep the remaining "distribution/maintance/java/source" leaving out the last forwardslash.

I've changed the way ive approched this so i dont need this solution but id be interested to see if there is a solution to this problem as im sure someone else may need it at some stage.

thanks again for your help
dexter

 
You have to download an addition jar from and put the jar into
c:\ant161\lib folder.

I myself use ant-contrib-1.0b3.jar
I am not good at regular expression, so the name each folder
is unique.
<?xml version="1.0" encoding="UTF-8"?>
<project name="ikey" default="ssget">
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="/usr/share/java/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<propertyregex property="first.result"
input="distribution/maintance/java/source/nameOfModule"
regexp=".*/"
replace=""
casesensitive="false" />
<propertyregex property="second.result"
input="distribution/maintance/java/source/nameOfModule"
regexp="/${first.result}$"
replace=""
casesensitive="false" />
<target name="ssget">
<echo message="${second.result}"/>
</target>
</project>
 
Write a simple Java program and call the program with ant may be a simple solution instead of complicated regular expression.
But it is not so pretty to do so.
 
Thats great, that does the trick.

Thanks again for the help
Dexter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top