File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Regex matching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex matching" Watch "Regex matching" New topic

Regex matching

Tim Sparg
Ranch Hand

Joined: Dec 29, 2006
Posts: 40
Hi guys

I'm having fun with regex, and hope somebody can help me out.

I have a string that will be along the lines of "/bleh/yadda/{userCode}/{blah}"
I'm trying to use regex to isolate userCode and bleh

Currently I have

which returns me '{userCode}'

Now I've got 2 issues here.
Firstly I can't seem to get the pattern to pick up multiple matches ie - '{blah}'
I've tried

and variations thereof, but then the whole pattern seems to go out of wack.

Secondly, is there any way that I can remove the { and } characters from being returned from the match?
So I still want the characters in the string, I just don't want them as part of the result

There is no insanity so devastating in man's life as utter sanity
Matthew Brown

Joined: Apr 06, 2010
Posts: 4544

I'm not sure what you mean by multiple matches here - can you be more specific? Do you just mean picking up the {userCode} and the {blah} in the same match?

Regarding just returning the bit matched, you can use brackets to group the bits you want to return. Then use whch returns a specified group (with group(0) returning the entire pattern).

Anyway, try this:

Then pull out the bits you want with and

Any good?
Tim Sparg
Ranch Hand

Joined: Dec 29, 2006
Posts: 40

Thanks, that was exactly what I was looking for

I agree. Here's the link:
subject: Regex matching
It's not a secret anymore!