2
4

4 に答える 4

3

Escape the [ within the negated character class. Although this shouldn't be necessary inside of a character class, clearly Java is having an issue with it, and it does not change the meaning of the character class to escape characters that shouldn't have a special meaning within a character class.

Try the following:

(\[.+?\][^\[]+)

Or for the Java code:

Pattern pattern = Pattern.compile(“(\\[.+?\\][^\\[]+)”);
于 2012-08-03T15:49:49.067 に答える
1

You need to escape the square bracket it just like you escaped them earlier:

(\\[.+?\\][^\\[]+)

The runtime exception is being caused because the RegEx parser sees [^[] as having an unclosed bracket.

于 2012-08-03T15:49:40.813 に答える
0

You need to excape the bracket, this should work :

[^\\[]
于 2012-08-03T15:50:00.137 に答える
0

The Java implementation seems to have a bug.

Normally, regex does not require you to escape it, but try escaping it anyway.

(\[.+?\][^\[]+)
"(\\[.+?\\][^\\[]+)"

It could be considered good practice to escape special characters, even if they don't need to be. It also helps avoid bugs like this.

于 2012-08-03T15:50:16.367 に答える