This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
String pat = "([^a-zA-z0-9])"; String escape= "\\\\$1"; //i am not sure what this is?? Pattern p = Pattern.compile(pat); Matcher m = p.matcher(""); m.reset(Me); String result= m.replaceAll(escape);
Thanks, Cathy.
Darin Niard
Ranch Hand
Joined: Jun 08, 2004
Posts: 118
posted
0
First of all, you don't really need the Pattern/Matcher code at all if you are just doing a replaceAll.
Java has a special class of characters: \p{Punct}
[ July 09, 2004: Message edited by: Darin Niard ]
Cathy Song
Ranch Hand
Joined: Jul 01, 2003
Posts: 92
posted
0
Hi Darin,
Thanks a lot for your reply. But I want to escape the punctuation not remove them. How can I do this.
Thanks, Cathy. [ July 09, 2004: Message edited by: Cathy Song ]
Darin Niard
Ranch Hand
Joined: Jun 08, 2004
Posts: 118
posted
0
I am not sure what you mean by escape in this context.
(The link in your post is broken.) [ July 09, 2004: Message edited by: Darin Niard ]
If the problem is inserting into the database you can use PreparedStatement. PreparedStatement takes care of quotes that may be a part of the String.
Alan Moore
Ranch Hand
Joined: May 06, 2004
Posts: 262
posted
0
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?
Cathy Song
Ranch Hand
Joined: Jul 01, 2003
Posts: 92
posted
0
Thanks Alan and everyone else for your inputs. You guys rock!!