Antoine Antoine - 1 month ago 5
Java Question

Java: regular expression: difference between "\\/" and "/"

Is there any difference between regular expressions

x1 = "\\/"
and
x2 = "/"
?

I could not find and strings
s
, such that
s.split(x1)
would not be equal to
s.split(x2)
. (The same holds, when I replace
/
by, e.g.,
a
.) I am on Windows.

Answer

No, there is no difference. x1 is actually \/ but / doesn't need backslash escaping (unlike \) and it's not a special string (tab, new line etc.)

String t = "th/is i/s a /test \\/t";
String r1 = "/";
String r2 = "\\/";
String[] t1 = t.split(r1);
for (int i = 0; i < t1.length; i++) {
    System.out.println(t1[i]);
}

OR

String[] t2 = t.split(r2);
for (int i = 0; i < t2.length; i++) {
    System.out.println(t2[i]);
}

these two produce the same output.

Comments