• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex matching

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks, that was exactly what I was looking for


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic