我需要逃避(没有输出的字符变成除了......之外的任何东西。
一
B'/ P>
任何帮助都非常感谢!
任意输入:
“a”+“\ n”+“(”+“b”
期望的输出:
一
B'/ P>
//Here are the attempted work-arounds that failed
40 String test = "a"+"\n("+"b";
41 String[] testSplitted = test.split("\n"+"(");
42 System.out.println(testSplitted[0]);
43 System.out.println(testSplitted[1]);
// ("\n"+"\(") ILLEGAL ESCAPE CHARACTER
// ("\n\(") ILLEGAL ESCAPE CHARACTER
// ("\n(") INVALID REGULAR EXPRESSION: UNCLOSED GROUP
// ("\n\\(") Output: a \(b (Desired Output: a b)
// ("\n"+"[(]") Output:
a [(]b Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at inputhandler.InputHandler.main(InputHandler.java:43)
Java Result: 1
// ("\n"+"(") Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 2
(
^
at java.util.regex.Pattern.error(Pattern.java:1924)
at java.util.regex.Pattern.accept(Pattern.java:1782)
at java.util.regex.Pattern.group0(Pattern.java:2857)
at java.util.regex.Pattern.sequence(Pattern.java:2018)
at java.util.regex.Pattern.expr(Pattern.java:1964)
at java.util.regex.Pattern.compile(Pattern.java:1665)
at java.util.regex.Pattern.<init>(Pattern.java:1337)
at java.util.regex.Pattern.compile(Pattern.java:1022)
at java.lang.String.split(String.java:2313)
at java.lang.String.split(String.java:2355)
at inputhandler.InputHandler.main(InputHandler.java:41)
Java Result: 1
答案 0 :(得分:16)
要转义(
,您需要两个反斜杠,因为反斜杠是Java字符串中的特殊字符,需要自行转义。所以,它变成:\\(