Java Regular Expression Issue - java

The following exception is thrown when this [0-9]+_[0-9]+_[0-9]+(_[0-9]+){0,1} regex is used.
Caused by: java.util.regex.PatternSyntaxException: Unclosed counted closure near index 31
[0-9]+_[0-9]+_[0-9]+(_[0-9]+){0
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.closure(Pattern.java:2759)
at java.util.regex.Pattern.group0(Pattern.java:2537)
at java.util.regex.Pattern.sequence(Pattern.java:1806)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
I am reading this from a XML File.

You probably want this:
[0-9]+_[0-9]+_[0-9]+(_[0-9]+)?

You have to escape commas with backslash:
[0-9]+_[0-9]+_[0-9]+(_[0-9]+){0\,1}

Related

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)+"\\\\");

antlr4 compile error: Serialized ATN data element out of range

antlr4.5, target Java, jdk1.6.
I compiled a .g4 combined file, and got this error:
Exception in thread "main" java.lang.UnsupportedOperationException: Serialized ATN data element out of range.
at org.antlr.v4.runtime.atn.ATNSerializer.serialize(ATNSerializer.java:370)
at org.antlr.v4.runtime.atn.ATNSerializer.getSerialized(ATNSerializer.java:547)
at org.antlr.v4.codegen.model.SerializedATN.<init>(SerializedATN.java:46)
at org.antlr.v4.codegen.model.Recognizer.<init>(Recognizer.java:87)
at org.antlr.v4.codegen.model.Lexer.<init>(Lexer.java:51)
at org.antlr.v4.codegen.OutputModelController.lexer(OutputModelController.java:176)
at org.antlr.v4.codegen.OutputModelController.buildLexerOutputModel(OutputModelController.java:129)
at org.antlr.v4.codegen.CodeGenerator.generateLexer(CodeGenerator.java:144)
at org.antlr.v4.codegen.CodeGenPipeline.process(CodeGenPipeline.java:73)
at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:429)
at org.antlr.v4.Tool.process(Tool.java:379)
at org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:346)
at org.antlr.v4.Tool.main(Tool.java:193)
at com.dicp.fdsl.antlr.FDSLCompiler.main(FDSLCompiler.java:13)
What does this error mean?
I had a similar problem, so I began commenting out lines in my grammar until the error went away.
In my case, the problem was a typo in my rules for numeric literals:
fragment DIGIT : '0'..'9 ' ;
Changing that to either of the following made the error go away:
fragment DIGIT : '0'..'9' ;
fragment DIGIT : [0123456789] ;
It seems likely that the character range from '0' to '9 ' is interpreted as Unicode values, and it produces an unexpectedly large range. This is consistent with the discussion in the ANTLR group at https://github.com/antlr/antlr4/issues/840.

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['

Illegal Escape Character "\("? [duplicate]

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

Categories