4

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 _

2
  • 3
    My guess is that you have an unclosed character class Commented Jul 12, 2013 at 20:01
  • Take a look at the section titled "Metacharacters Inside Character Classes" on this page explaining character classes, where they address [^]x] as an example that may explain your error well. Commented Jul 12, 2013 at 20:09

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

[^] is a common shorthand for any character, including newlines in JavaScript (because there is no Pattern.DOTALL option there). So it's probably a simple ., together with the Pattern.DOTALL option that he needs.
Can you help me with this part of question?
I would think you only need _? to allow 0 or 1 underscores. I don't think you need the braces as you put [_]? but I would suppose that should work too. I tried it here regexplanet.com/advanced/java/index.html and it works for both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.