regex - Is there any need to escape the slash('/') character for regular expressions in Java -
i have 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 slash system.out.println(matcherofslashcontainingbackslash.matches()); system.out.println(matcherofslashnotcontainingbackslash.matches());
my questions:
what difference (from java perspective) between 2 patterns, or there difference?
is '/' character plain character regex(not special character ']' is) ,from java perspective?
the java version on run 1.8
this question different others, since makes clear patterns "\\/" , "/" same java programming language.
thank much!
/
not special in java regex, in javascript have syntax /regex/flags
. java allows escaping characters if don't require it, make such regex more portable. pattern.compile("\\/")
, pattern.compile("/")
behave same.
btw ]
not special character. if not part of [...]
construct don't need escape it, allowed (at least in java, not sure other regex flavors).
Comments
Post a Comment