File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes regex how to find UP TO a character but not include this in matcher.group() result Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "regex how to find UP TO a character but not include this in matcher.group() result" Watch "regex how to find UP TO a character but not include this in matcher.group() result" New topic
Author

regex how to find UP TO a character but not include this in matcher.group() result

J Westland
Ranch Hand

Joined: Apr 06, 2009
Posts: 40
Hi -

I want to do the following:

In lines looking like so:

metadata\Metadata00060_v00004\gmd\gmd.jar<location>

I want to find only:

metadata\Metadata00060_v00004\gmd\gmd.jar

My current pattern is:

Pattern meta = Pattern.compile("Metadata.*?<");

That leaves me with the < in my search which I don't want. I did a quick Google and look through the Java tutorials JavaScript has something called "lookahead" which is want I want, but doesn't look Java has this.

How should I go about to do this?

Thanks,

Jawine


Woohoo passed SCJP 1.6, that's the theory exam passed now for the practice ;)
Mazer Lao Tzu
Ranch Hand

Joined: Jan 20, 2010
Posts: 35
Simply put parentheses around the part of your pattern that you wish to find. Then, instead of using matcher.group(), use matcher.group(1).

For example:


If the above was given the input "barbaztastic.jar<location>" then result would be "baztastic.jar" at the end.



-- Mazer
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

Check out the Javadoc of java.util.regex.Pattern and search for (positive) lookahead.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
J Westland
Ranch Hand

Joined: Apr 06, 2009
Posts: 40
@Mazer Lao Tzu: Thank you that works
@Rob Prime: I just remember now I did indeed see it in the API but I couldn't get it to work.

Regex was covered in the SCJP but of course only the basics, I will try around a bit more with what you both told me, the only way to learn really
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19214

J Westland wrote:@Rob Prime: I just remember now I did indeed see it in the API but I couldn't get it to work.

What regex did you come up with, using a positive lookahead? Then we can tell you what you did wrong.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: regex how to find UP TO a character but not include this in matcher.group() result
 
Similar Threads
RESTful service - standard output elements, structure ?
XMP Technology
Need help sharing one Eclipse workspace across platforms (different .metadata folders required)
Design Review
reading audio metadata via JAVA API ?