• 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

The appendReplacement and appendTail

 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alan .
 
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
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

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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic