• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex matching

 
Tim Sparg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks, that was exactly what I was looking for


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic