jQuery in Action, 3rd edition
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 4541

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 matcher.group(int) whch returns a specified group (with group(0) returning the entire pattern).

Anyway, try this:

Then pull out the bits you want with matcher.group(1) and matcher.group(2).

Any good?
Tim Sparg
Ranch Hand

Joined: Dec 29, 2006
Posts: 40

Thanks, that was exactly what I was looking for

Consider Paul's rocket mass heater.
subject: Regex matching
It's not a secret anymore!