This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Parsing escape characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing escape characters" Watch "Parsing escape characters" New topic
Author

Parsing escape characters

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I am busy building a translator for the website here at work. I now have the strings that need to be translated, but I need to parse them to get them into a more user friendly format.

So I have the string
"'hello '. CATNAME . ' rachel\'s cat'"

For php, I need to keep the single quotes that mark the beginning and end of the php string.

So what I am doing is reading for every single quote to find the beginning and end of these strings. But the \' is also getting read as a plain '.

I thought about replacing all \ with \\ but I can't use the replaceAll function because it doesn't pick up the \ when they are used as escape characters

So enough rambling. And here is a plain question because if I know this I'm sure I can solve the problem.

If I have a string = "'hello '.CATNAME.' rachel\'s cat'";
How can I get it to be = "#hello #.CATNAME.# rachel\'s cat#";


Many kind regards and a wonderful weekend!
Cheers, Rachel
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326

[ November 05, 2004: Message edited by: Ray Stojonic ]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
You can try this.

I found that I couldn't do it in less that four steps.



s is the string, t is a temporary version of string, n is the befinnings of a new string, and f is the final string.

hack hack hack hack hack hack hack
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
My pseudocode works just fine, but jefff got me thinking about how replaceAll is a regex savvy method, so:

"'hello '.CATNAME.' rachel\\'s cat'".replaceAll( "([^\\\\])'|^'", "$1#" );

output: #hello #.CATNAME.# rachel\'s cat#
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Ray,

nice.

I spun my wheels trying to get the regex down and couldn't do it.

good job.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there.

I tried all except the regex one (I'm gonna play with that now.) But all of the solutions center around being able to find the \ and as I said before, when it's used as part of an escape char, myString.indexOf the \ will return a -1. So a solution based on finding this doesn't work.

Ok, going to play with the regex one now.

Cheers and thanks for the suggestions!!
Rachel
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
OK, how bizarre is this. When I type
String a = "hello rachel\'s cat"

Java picks the escape char up as a string literal.

But when I read from a file, I can get the \. So problem solved since I am reading it from files!

Thanks to everyone for the suggestions!
Cheers,
Rachel
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Parsing escape characters