• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Escaping special characters regex/function help

 
Sean Chang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I needed a little help writing a function in java that does the follow for espacing, I want to use regular expressions, I need the following characters to be replaced by a “~”

The special characters are replaced as follows:

1. Newline, backspace, Control-M, carriage return, vertical tab characters are each replaced with "~"

2. Double quotes (") are replaced with single quotes (')

3. Then single quotes are escaped by inserting a backslash before each one.

Can someone give me a hand writing this? I don’t know regular expressions so it’s difficult for me to get going on this.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good place to learn regular expressions is in the Java™ Tutorials.
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don’t know regular expressions so it’s difficult for me to get going on this.


I am a big believer that you should never use code that you don't understand. So, if you don't know regex, then you probably shouldn't use it.

Take a look at the string replace() methods. It will do what you want, and you don't need to know regex.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic