0

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.

Antoine
  • 862
  • 7
  • 22
  • You will have a difference when you use reserved characters like '.' '\', etc – Cédric O. Oct 26 '16 at 11:32
  • 1
    try to print the content of `"\\/`, `"/"` and you might find the difference yourself – SomeJavaGuy Oct 26 '16 at 11:32
  • This is actually a really good question. – ItamarG3 Oct 26 '16 at 11:36
  • You ask what the difference between `\/` and `/` patterns are. None as you can see for yourself at http://regex101.com. – Wiktor Stribiżew Oct 26 '16 at 11:37
  • @KevinEsche This is precisely the reason why I am asking this question, since the first one gives `\/`, while the second one `/`. However, if I split "a\\/b", I get `[a\, b]` in both cases, which I find weird. – Antoine Oct 26 '16 at 11:37
  • 2
    Reopened as the duplicate was inappropriate. The topic there are regexes in general and, even after going through that huge pile of text, I still couldn't find an explicit statement that answers this question. – Marko Topolnik Oct 26 '16 at 11:57
  • @MarkoTopolnik Neither could I. – Antoine Oct 26 '16 at 13:54

1 Answers1

0

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
ItamarG3
  • 4,092
  • 6
  • 31
  • 44