java regex illegal escape character error not occurring from command line arguments -
this simple regex program
import java.util.regex.*; class regex { public static void main(string [] args) { system.out.println(args[0]); // #1 pattern p = pattern.compile(args[0]); // #2 matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { system.out.println(m.start()+" "+m.group()); } } } invoked java regex "\d" "sfdd1" compiles , runs fine.
but if #1 replaced pattern p = pattern.compile("\d");, gives compiler error saying illegal escape character. in #1 tried printing pattern specified in command line arguments. prints \d, means getting replaced \d in #2.
so why won't throw exception? @ end it's string argument pattern.compile() taking, doesn't detect illegal escape character then? can please explain why behaviour?
a backslash character in string literal needs escaped (preceded backslash). when passed in command line string not string literal. compiler complains because "\d" not valid escape sequence (see escape sequences character , string literals ).
Comments
Post a Comment