• 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

escape character nightmare - please help

 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I'm trying to assign the value

\"

to a String. That is, I want the string to be backslash + double quote. \" just gives the double quote, \\" becomes an unterminated string, and \\\" is back to the double quote without the backslash. I'm pretty sure that unicode would have the same result. The must be some way to do this, right?

Thanks in advance,
Yuriy
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works fine:

String s = "\\\"";

From your message, that appears to be one of the things you tried, but perhaps you made a typo?
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, that does work. Actually what I am really trying to do is replace all occurances of double quotes in a String with the escaped double quote. So more along the lines of

String test = "\"abc";
System.out.println(test.replaceAll("\"", "\\\""));

this just prints out "abc
I'm guessing that this is a regular expression problem then?

Thanks in advance,
Yuriy
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yuriy Zilbergleyt:

I'm guessing that this is a regular expression problem then?



Yes. It's tricky because the backslash is meaningful to both Java and the regexp parser. To do this, first write the actual expression you want to replace with:

\\"

Then escape each of the special characters so they'll pass through the Java parser untouched:

\\\\\"

So you need to say

System.out.println(test.replaceAll("\"", "\\\\\""));

Five slashes!
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thank you so much!

Yuriy
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your subject name aptly names your problem. When dealing with regular expressions, escape characters are definitely nightmarish!

Layne
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps to read regular expressions containing special characters from a file or a textfield, and to control them by printing them out.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic