This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
First off, there's a typo in the regex you copied from that web page: the second 'z' should have been a capital 'Z'. Due to that error, the code will silently fail to escape several important characters. Also, it will unnecessarily escape whitespace and any letters or digits that don't happen to be ASCII characters. As Darin said, you would be much better off using a regex that explicitly matches ASCII punctuation characters:In the replacement string, the '$' character followed by a digit serves as a placeholder for whatever was matched by the corresponding capturing group in the regex. A capturing group is just a part of the regex that's enclosed in parentheses; they're numbered according to the order in which they occur in the regex. "$0" is a special case that inserts the whole match.
If you want to put a literal '$' in the replaced text, you have to escape it with a backslash. That means a backslash, too, is a special character in the replacement string. Since it's also a special character for String literals, you have to double-escape it, i.e., put four backslashes in the replacement string to get one to appear in the output.
Does that answer your question, Cathy?
Joined: Jul 01, 2003
Thanks Alan and everyone else for your inputs. You guys rock!!