Illegal Escape Character "\("? [duplicate] - java

This question already has answers here:
Why does this Java regex cause "illegal escape character" errors?
(7 answers)
Closed 3 years ago.
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

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: \\(

Related

How to escape dot character using String.format

Using Java 8, I would like to get this code block :
System.out.println(String.format("Hello %.", "world"));
Results in :
Hello world.
But unfortunately gets :
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
at java.util.Formatter.checkText(Formatter.java:2579)
at java.util.Formatter.parse(Formatter.java:2565)
at java.util.Formatter.format(Formatter.java:2501)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
How could I escape this dot character, which seems to have a special meaning ?
System.out.println(String.format("Hello %s.", "world"));

How to separate a string into multiple lines based on a separator but not if it is preceded by '?' [duplicate]

This question already has answers here:
Regex lookahead, lookbehind and atomic groups
(5 answers)
Closed 6 years ago.
I am trying to separate a string into multiple lines based on separator ' but if there's a ? character before ' I want the data to remain in the same line.
Initial String:
HJK'ABCP?'QR2'SER'
I am able to print the lines like:
HJK'
ABCP?'
QR2'
SER'
But I want the output as:
HJK'
ABCP?'QR2'
SER'
You need a negative lookbehind (http://www.regular-expressions.info/lookaround.html) :
String str = "HJK'ABCP?'QR2'SER'";
System.out.println(str);
System.out.println("---------------");
System.out.println(str.replaceAll("'", "'\r\n"));
System.out.println("---------------");
System.out.println(str.replaceAll("(?<!\\?)'", "'\r\n"));
It returns :
HJK'ABCP?'QR2'SER'
---------------
HJK'
ABCP?'
QR2'
SER'
---------------
HJK'
ABCP?'QR2'
SER'
Use this regex (?<!\?)' in split funtion
String s="HJK'ABCP?'QR2'SER'";
System.out.println(s.replaceAll("(?<!\\?)'","\r\n"));

Regex error on Java

I created regex for extracting php exception message fields
(\w+.*)|\G(?!\A)\s*#\d+\s+(\S+\.php)\((\d+)\):\s(\w+.*)#012|#\d+\s{(\w+)}
Demo Links : https://regex101.com/r/xI6cR0/2
Error Message:
Illegal repetition near index 66 (\w+.*)|\G(?!\A)\s*#\d+\s+(\S+\.php)\((\d+)\):\s(\w+.*)#012|#\d+\s{(\w+)} ^
You need to escape all \ again by \\ for it to work.So your \w would become \\w.You also need to escape {}.So it would be
(\\w+.*)|\\G(?!\\A)\\s*#\\d+\\s+(\\S+\\.php)\\((\\d+)\\):\\s(\\w+.*)#012|#\\d+\\s\\{(\\w+)\\}

Replace backslash in string

I have a String in which I am trying to replace the number enclosed by two backslashes. For example: \10\ , I am trying to replace that with 10. I am currently using this regex to do that:
String texter = texthb.replaceAll("\\.+\\", "\\"+String.valueOf(pertotal + initper)+"\\");
This line is giving the following error:
Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 4
.+\
I know it is because the regex is wrong. What is the proper way to accomplish this? Thanks in advance.
Use four backslashes to match a single backslash character.
String texter = texthb.replaceAll("\\\\.+?\\\\", "\\\\"+String.valueOf(pertotal + initper)+"\\\\");

quotes in the java regular expressions

I get a String from the JSP, containing [", e.g.
["Bulgaria
I would like to replace all the [" occurrences for [', but I don't know exactly how to do it...
I just tried:
str = str.replaceAll("[\\\"", "['");
with the result
java.util.regex.PatternSyntaxException: Unclosed character class near index 2 [\"
and
html = html.replaceAll("[\"", "['");
with the result
java.util.regex.PatternSyntaxException: Unclosed character class near index 1 [" ^
any help will be appreciated
Try this:
str.replaceAll("\\[\"", "['");
You need \\ to escape in java regex and [ is a special character in java regex, thus the \\ in front of it. " is a special character in strings so you only need one \ to escape it.
"Test[\"".replaceAll("\\[\"", "['"); // Test['

Categories