stuck in regex

G

Guest

I want to replace all
\ with \\ and all
" with \"
in a String.

I use this line of code:
text = text.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"");

But i have PatternSyntaxException
Why?
 
H

HK

I use this line of code:
text = text.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"");

But i have PatternSyntaxException


"\\" results in a 1 character string containing a backslash
only. This is not a valid regular expression.

Harald.
 
K

Kevin

I am also kind of stuck in the regex, I sometimes use this function to
do the string / substring replacement work.

public String stringReplaceAllCaseSensitive (String orig, String
from, String to)
{
if ((from==null) || (from.length()==0))
{
println("\tWarning in stringReplaceAllCaseSensitive, from
string is empty. Ignored. ");
return orig;
};
if (from.compareTo(to)==0)
return orig;
//
int fromLength = from.length();
//
int start = orig.indexOf (from);
if (start <0)
return orig;
//
boolean greaterLength = (to.length() >= fromLength);

StringBuffer buffer;
// If the "to" parameter is longer than (or
// as long as) "from", the final length will
// be at least as large
if (greaterLength)
{
buffer = new StringBuffer(orig.length());
}
else
{
buffer = new StringBuffer();
};
//
char [] origChars = orig.toCharArray();
int copyFrom=0;
while (start != -1)
{
buffer.append (origChars, copyFrom, start-copyFrom);
buffer.append (to);
copyFrom=start+fromLength;
start = orig.indexOf (from, copyFrom);
}
buffer.append (origChars, copyFrom, origChars.length-copyFrom);

return buffer.toString();
}
//
 
H

HK

Kevin said:
I am also kind of stuck in the regex, I sometimes use this function to
do the string / substring replacement work.

public String stringReplaceAllCaseSensitive (String orig, String
from, String to)
{
if ((from==null) || (from.length()==0))
{
println("\tWarning in stringReplaceAllCaseSensitive, from
string is empty. Ignored. ");
return orig;

Guess were this message ends up in a server application.
This should rather be either an IllegalArgumentException
or the docs should state that empty from is silently
ignored.
if (from.compareTo(to)==0)
return orig;
Why not from.equals(to) ?

http://java.sun.com/j2se/1.5.0/docs...va.lang.CharSequence, java.lang.CharSequence)

Harald.
 
T

Tony Morris

I want to replace all
\ with \\ and all
" with \"
in a String.

I use this line of code:
text = text.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"");

But i have PatternSyntaxException
Why?

Here are a couple of tips.
A literal Java backslash looks like this: "\\"
A literal regex backslash looks like this: \\
Therefore, a literal regex backslash using Java looks like this: "\\\\"
 
H

hiwa

I want to replace all
\ with \\ and all
" with \"
in a String.

I use this line of code:
text = text.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"");

But i have PatternSyntaxException
Why?
Backslash requires special treatment BOTH in regex string
and Java String. See the example program:

public class ReplaceTest{
public static void main(String[] args){
String str = "\\he said \"I love you.\"\\";

System.out.println("original: " + str);

/* regex str for a single BS is \\, so the Java String for
that is "\\\\" */
str = str.replaceAll("\"", "\\\\\""); //dq -> BS + dq
System.out.println("result1 : " + str);
/* read the above comment for BS regex str */
str = str.replaceAll("\\\\", "\\\\\\\\"); //BS -> double BSs
System.out.println("result2 : " + str);
}
}
 
A

Alan Moore

I want to replace all
\ with \\ and all
" with \"
in a String.

I use this line of code:
text = text.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"");

But i have PatternSyntaxException
Why?

The arguments to replaceAll get processed twice: first by the
compiler, then by the Pattern (first argument) and Matcher (second
argument) classes, which do the actual matching and replacing. Both
of those classes also treat the backslash as an escape character. So,
to match a literal backslash character, or to insert one into the
replacement string, you have to double it, then double it again. So
replacing a single backslash with a double backslash looks like this:

text = text.replaceAll("\\\\", "\\\\\\\\");

The double-quote character, OTOH, has to be escaped for the compiler,
but it isn't special to the regex classes, so you only need one
backslash to escape it. Of course, to add a backslash in front of it,
you have to use four of them, plus the one to escape the quote:

text = text.replaceAll("\"", "\\\\\"");

Instead of doing in two passes, though, you can use the power of
regexes to do it in one:

text = text.replaceAll("[\"\\\\]", "\\\\$0");

The regex, "[\"\\\\]", is a character class that matches either a
quote or a backslash, and the $0 in the second argument gets replaced
with whatever was matched by the regex. For more info, check out this
site:

http://www.regular-expressions.info/
 
A

Aquila Deus

I want to replace all
\ with \\ and all
" with \"
in a String.

I use this line of code:
text = text.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"");

But i have PatternSyntaxException
Why?

hmmmm... escape char nightmare, it happens in emacs too :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top