• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex and replacing groups

 
Tim Patton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out how to replace just part of a match using the java regex classes (or some other set). Say I have a regex like this:

(\\d*)-(\\d*)

It will match strings like this: 4-3 100-5656 1-1

I would like to use string replace on the 2nd group, but I am not sure how.

If I use string.replace(matcher.group(2), ....) then it will not work correctly in the case of 1-1. I can't use backreferences in string.replace since the '-' isn't included in a group (other than 0). Is this possible with any regex api for java? I know I could include everything in a group, but for my real world usage that could get too complicated.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell what you're trying to do here. Replace with what? Can you show an example of what you want the result to look like?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic