4 に答える
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(“(\\[.+?\\][^\\[]+)”);
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.
You need to excape the bracket, this should work :
[^\\[]
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.