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:

  1. what difference (from java perspective) between 2 patterns, or there difference?

  2. 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

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -