简体   繁体   中英

java.util.regex.PatternSyntaxException: Unclosed character class near index

I am writing a regex in java but i am getting an error when I run the program.

private final static Pattern QUOTE_VALUE = Pattern.compile("[_]?([a-zA-Z0-9_]+)=(\"[^]*\"),");
// Then later on down the road......
Macher m = QUOTE_VALUE.matcher(myString);
while (m.find()){
  System.out.println("Found " + m.group(1) + " " + m.group(2));
}

I want to make my regex to match these sample values.

_MyKey="ID IN [ "ABC" ]",  // Note - it has a comma after the ]
_MyKey="ID IN [ ""XYZ"" ]",   // Note - it has a comma after the ]

I tried it with online regex helper - and my regex actually works fine. But when I run the program in , i get this error:

Caused by: java.util.regex.PatternSyntaxException: Unclosed character class near index 28
[_]?([a-zA-Z0-9_]+)=("[^]*"),

Another question is, how do i format the regex so i can also match it with this String?

MyKey="ID IN [ "ABC" ]",  // without the _
_MyKey="ID IN [ "ABC" ]",  // with the _

Thanks.

[EDIT]

Can you help me with this part of question?

Another question is, how do i format the regex so i can also match it with this String?

MyKey="ID IN [ "ABC" ]", // without the _ _MyKey="ID IN [ "ABC" ]", // with the _

This part:

[^]

Needs to either be

[\\^]*

if you are looking for a sequence of zero or hat characters. ( Note that the first backslash is there to tell the Java string parser that the 2nd backslash needs to stay in the string. The end result is that the regex parser sees just one backslash and uses it to say the hat character need to be part of the character class identified by the square braces.)

Or you are missing something you don't want as in:

[^b]*

to match zero or more non-b characters

Considering what you want to match, try [^"]* or maybe just .*

If you do need to match across line endings, use the .* and put ?s at the front of the regex to force the java regex matcher into a mode that allows the dot to match all characters including the newline.

Thanks to @TimPietzcker for the javascript note. This suggests that the online tool was not a Java regex checker but maybe a javascript regex checker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM