0

My regular expression:

java.util.regex.PatternSyntaxException: Unclosed character class near index 14
[^]*?¥(\d*.\d*)
              ^

But I can't find any flaw in my expression and it works well on http://regexr.com/.

My intention for [^]:

Use [^] to match any character because . only match any character except line break.

Call code:

Pattern pattern = compile(regex);

StackTrace:

0 = {StackTraceElement@12872} "java.util.regex.Pattern.error(Pattern.java:1955)"
1 = {StackTraceElement@12873} "java.util.regex.Pattern.clazz(Pattern.java:2548)"
2 = {StackTraceElement@12874} "java.util.regex.Pattern.sequence(Pattern.java:2063)"
3 = {StackTraceElement@12875} "java.util.regex.Pattern.expr(Pattern.java:1996)"
4 = {StackTraceElement@12876} "java.util.regex.Pattern.compile(Pattern.java:1696)"
5 = {StackTraceElement@12877} "java.util.regex.Pattern.<init>(Pattern.java:1351)"
6 = {StackTraceElement@12878} "java.util.regex.Pattern.compile(Pattern.java:1028)" 
...

EDIT:

[^.]*?¥(\d*.\d*) works. It seems java.util.regex doesn't support [^] to match any character.

If any one is willing to write some explanation for that, I will accept it as answer.

5
  • @hwnd [^] match any character but . match any character except line break Commented Aug 12, 2015 at 3:42
  • 1
    This isn't Javscript ... Commented Aug 12, 2015 at 3:43
  • Please provide sample input with expected output so that we can provide you a better regex expression. Commented Aug 12, 2015 at 3:58
  • 1
    @hwnd: I think your answer applies to this case. Commented Aug 12, 2015 at 4:01
  • [^] is only valid in JavaScript. In Java, [^] is an unclosed character class that negates the character set that matches ] (plus anything that comes after it) Commented Aug 12, 2015 at 4:02

1 Answer 1

3

The problem is that you are using the wrong tool to test your regex. RegExr v2 uses JavaScript regex, while RegExr v1 uses Flex regex, both have different parsing rule compared to Java regex.

While [^] is valid in JavaScript, it is parsed as an (incomplete) negated character class in Java, which contain literal ] (and whatever follows).

Java does not accept empty character class (or negated empty character class), so the first ] in character class (i.e. [] or [^]) does not close the character class, and ] is treated as literal character instead.

To make . match any character without exception, you might want to use Pattern.DOTALL flag:

Pattern pattern = Pattern.compile(".*?¥(\\d*.\\d*)", Pattern.DOTALL);
Sign up to request clarification or add additional context in comments.

2 Comments

What is unclosed negated character class, is there any document about that?
@Sayakiss: [^] is not a valid character class in Java by itself, and it needs to be close with another ]. Java does not accept empty character class (or negated thereof), so ] at the beginning of the character class will not close the character class, and treated as literal character.

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.