Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to appendReplacement on a Matcher group instead of the whole pattern?

Tags:

I am using a while(matcher.find()) to loop through all of the matches of a Pattern. For each instance or match of that pattern it finds, I want to replace matcher.group(3) with some new text. This text will be different for each one so I am using matcher.appendReplacement() to rebuild the original string with the new changes as it goes through. However, appendReplacement() replaces the entire Pattern instead of just the group.

How can I do this but only modify the third group of the match rather than the entire Pattern?

Here is some example code:

Pattern pattern = Pattern.compile("THE (REGEX) (EXPRESSION) (WITH MULTIPLE) GROUPS"); Matcher matcher = pattern.matcher("THE TEXT TO SEARCH AND MODIFY"); StringBuffer buffer = new StringBuffer();  while(matcher.find()){    matcher.appendReplacement(buffer, processTheGroup(matcher.group(3)); } 

but I would like to do something like this (obviously this doesn't work).

... while(matcher.find()){    matcher.group(3).appendReplacement(buffer, processTheGroup(matcher.group(3)); } 

Something like that, where it only replaces a certain group, not the whole Pattern.

EDIT: changed the regex example to show that not all of the pattern is grouped.

like image 392
cottonBallPaws Avatar asked Oct 15 '10 07:10

cottonBallPaws


People also ask

What does group do in matcher?

The group() method of Matcher Class is used to get the input subsequence matched by the previous match result.

What is pattern and matcher?

A pattern is a compiled representation of a regular expression. Patterns are used by matchers to perform match operations on a character string. A regular expression is a string that is used to match another string, using a specific syntax.

How does matcher group work java?

Java Matcher group() MethodThe group method returns the matched input sequence captured by the previous match in the form of the string. This method returns the empty string when the pattern successfully matches the empty string in the input.


1 Answers

I see this already has an accepted answer, but it is not fully correct. The correct answer appears to be something like this:

.appendReplacement("$1" + process(m.group(2)) + "$3"); 

This also illustrates that "$" is a special character in .appendReplacement. Therefore you must take care in your "process()" function to replace all "$" with "\$". Matcher.quoteReplacement(replacementString) will do this for you (thanks @Med)

The previous accepted answer will fail if either groups 1 or 3 happen to contain a "$". You'll end up with "java.lang.IllegalArgumentException: Illegal group reference"

like image 137
Warren Avatar answered Oct 13 '22 22:10

Warren