• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Escaping special characters regex/function help

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good place to learn regular expressions is in the Java™ Tutorials.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic