Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal Escape Character "\("? [duplicate]

I need to escape the ( character without the output becoming anything other than..

a

b

Any help greatly appreciated!

Arbitrary Input:

"a"+"\n" +"("+"b"

Desired Output:

a

b

//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
like image 993
user3025996 Avatar asked Nov 30 '22 20:11

user3025996


1 Answers

To escape ( you need two backslashes since the backslash is a special character in Java strings, and needs to be escaped itself. So, it becomes: \\(

like image 193
Keppil Avatar answered Dec 09 '22 09:12

Keppil