File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

escape punctuation in string

 
Cathy Song
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the URI Cathy intended to reference was http://javaalmanac.com/egs/java.util.regex/Escape.html

Moving this to the Intermediate forum...
 
K Anshul
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alan and everyone else for your inputs. You guys rock!!

-Cathy.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic