| 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.
|
 |
 |
|
|
subject: regex how to find UP TO a character but not include this in matcher.group() result
|
|
|