• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Help with Regular Expressions

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me how to get the replacement text for each matcher capturing group?

example: I want to replace "WARM tamales" with "WARMER tamales", using the search pattern "(HOT|COLD|WARM|)" and the replacement pattern "$1ER". This works fine, but I also want to be able to discover what was the final replacement text used for capturing group 1 (eg. in this example, it would be the text WARMER, which replaced the original text WARM).

Does this make sense? Can it be done?

PLEASE HELP!

edit: oh yeah, here's some code...


edit: Fixed the comments in the code (thanks Rob!)
 
author
Posts: 23892
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The replaceAll() method is merely a convenience method that uses the lower level find(), appendReplacement(), and appendTail() methods. An option is to not use replaceAll(), but use the lower level methods directly, to do the replacement, and to save a copy of the replacement.

The javadoc for the append methods has an example of how the three methods work together...

http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html

Henry
 
Sheriff
Posts: 22082
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I copied your code exactly as it is, and for me it printed "results: WARMER tamales".
 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:I copied your code exactly as it is, and for me it printed "results: WARMER tamales".



Yep. That's the correct result. The comments in the code were wrong (typo). Sorry.
 
Destroy anything that stands in your way. Except this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic