File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes escape punctuation in string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "escape punctuation in string" Watch "escape punctuation in string" New topic
Author

escape punctuation in string

Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
Hi All,

I am trying to escape all punctuation chars in a string,
but I am failing..please help. (I used this
url as reference http://javaalmanac.com/egs/java.util.regex/Escape.html)

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
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
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
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 ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I believe the URI Cathy intended to reference was http://javaalmanac.com/egs/java.util.regex/Escape.html

Moving this to the Intermediate forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
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


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
Thanks Alan and everyone else for your inputs. You guys rock!!

-Cathy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: escape punctuation in string