3

I have the following code snippet:

Pattern patternOfSlashContainingBackSlash=Pattern.compile("\\/");
Pattern patternOfSlashNotContainingBackSlash=Pattern.compile("/");

String slash = "/";

Matcher matcherOfSlashContainingBackSlash = patternOfSlashContainingBackSlash.matcher(slash);
Matcher matcherOfSlashNotContainingBackSlash = patternOfSlashNotContainingBackSlash.matcher(slash);

//both patterns match the slash
System.out.println(matcherOfSlashContainingBackSlash.matches());
System.out.println(matcherOfSlashNotContainingBackSlash.matches());  

My questions:

  1. What is the difference (from Java perspective) between the two patterns, or is there any difference?

  2. Is the '/' character just a plain character for regex(not a special character like ']' is) ,from Java perspective?

The java version on which I run this is 1.8

This question is different from the others, since it makes it clear that the patterns "\\/" and "/" are the same for Java programming language.

Thank you very much!

dj_frunza
  • 1,553
  • 3
  • 17
  • 28
  • As far as I know, it wont make much of a difference. Putting the escape slash in front of a character ussually means that the character has to be taken as the literal character... unless it has some special meaning. In the case of the literal, where the slash would be optional, it would not be necessary... IntelliJ also says it is a redundant character escape. – Wietlol Mar 28 '17 at 14:48
  • 2
    Possible duplicate of [List of all special characters that need to be escaped in a regex](http://stackoverflow.com/questions/14134558/list-of-all-special-characters-that-need-to-be-escaped-in-a-regex) – radoh Mar 28 '17 at 14:49
  • @radoh , I do not think this question is duplicated since it also clarifies the fact that there is no difference between "\\/" and "/" patterns.Thank you – dj_frunza Mar 28 '17 at 15:02

5 Answers5

4

/ is not a special character in Java regexes. "\\/" is equivalent to "/".

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
4

/ is not special in Java regex, it is in JavaScript where we have syntax /regex/flags. Java allows escaping characters even if they don't require it, probably to make such regex more portable. So Pattern.compile("\\/") and Pattern.compile("/") will behave the same.

BTW ] by itself is not special character. If it is not part of [...] construct you don't need to escape it, but you are allowed to (at least in Java, I am not sure about other regex flavors).

Pshemo
  • 122,468
  • 25
  • 185
  • 269
3

The forward slash / character is not a command character in Java Pattern representations (nor in normal Strings), and does not require escaping.

The back-slash character \ requires escaping in Java Strings, as it is used to encode special sequences such as newline ("\n").

E.g. String singleBackslash = "\\";

As the back-slash is also used to signify special constructs in Java Pattern representations, it may require double escaping in Pattern definitions.

E.g. Pattern singleBackSlash = Pattern.compile("\\\\");

See API for examples of Pattern constructs involving the back-slash.

Mena
  • 47,782
  • 11
  • 87
  • 106
2

There is no need to escape the slash character. The reference for Java's regex syntax is the Javadoc for class Pattern. There, it lists all the characters that have special meaning. The slash is not present in that list.

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
2

I think that the reason why people escape the / character is that they come from different regex flavours (like Javascript) where you can define a pattern enclosing it between two slashes and if you want to match one slash you need to escape it.

var pattern = /\d\/\d/; // Valid pattern of 2 digits divided by a slash, which must be escaped.

From a Java perspective, though,it is equivalent and thus there's no need to escape it.

Aurasphere
  • 3,841
  • 12
  • 44
  • 71