aspose file tools*
The moose likes Java in General and the fly likes The appendReplacement and appendTail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "The appendReplacement and appendTail" Watch "The appendReplacement and appendTail" New topic
Author

The appendReplacement and appendTail

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

What is the difference b/w appendReplacement and replaceAll methods in the java.util.regex.Matcher class? Thanks
[ December 17, 2004: Message edited by: Pradeep Bhat ]

Groovy
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
appendReplacement and appendTail are the methods that do the real work when you call replaceAll. Each time a match is found in the input, appendReplacement appends to the output all the text between the previous match and the current one, then it appends the replacement text. appendReplacement is also the method that processes the replacement text, converting $1, $2, etc. to the corresponding captured groups. When there are no more matches, appendTail appends whatever's left of the input (whatever followed the last match).

You use appendReplacement and appendTail directly when you want to process the input in some other--or additional--manner than replaceAll does. For instance, if you want to use the matched text as a key to look up the replacement value in a table, you have to write your own replaceAll equivalent. Here's an example I happened to have handy:
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Thanks Alan .
Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

I was searching for a good explanation of the method appendReplacement, further than the above posted there is this one which is very very good in my opinion.

http://www.java2s.com/Code/Java/Regular-Expressions/MatcherappendReplacement.htm

OCAJP 7, OCPJP 7
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Nick Widelec wrote:I was searching for a good explanation of the method appendReplacement, further than the above posted there is this one which is very very good in my opinion.

http://www.java2s.com/Code/Java/Regular-Expressions/MatcherappendReplacement.htm



To be blunt, this example is pretty bad. It is not only *NOT* "very very good", it is not even a valid example as it is being used wrong.

The appendReplacement() method is paired up with the appendTail() method. This example doesn't do that. And if you look at the output, you will see that it does more than just a simple replacement. It also truncates everything after the match (due to the missing appendTail() call). Unfortunately, it is only a period that has been truncated -- which probably went unnoticed.

Anyway, if this is a sign of the quality, I would highly recommend that you use a different site to learn Java.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
gMale Another
Greenhorn

Joined: Aug 09, 2013
Posts: 1
The code above from Alan Moore has two problems:
1) it doesn't take advantage of appendReplacement properly
2) it doesn't account for null pointers or unmatched properties

Here is a slight tweak to the code that resolves those issues:Of course, nowadays, there are plenty of frameworks that handle this kind of thing for us like Spring's PropertyPlaceholderHelper.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The appendReplacement and appendTail