[英]Why closing square bracket “]” doesn't require escaping in Java regex?
Consider the array: 考虑一下数组:
new Pattern[] {Pattern.compile("\\["),Pattern.compile("\\]") };
Intellij IDEA tells me that \\\\
is redundant and tells me to replace this with ]
eg the result is: Intellij IDEA告诉我
\\\\
是多余的,并告诉我将其替换为]
例如结果是:
new Pattern[] {Pattern.compile("\\["),Pattern.compile("]") };
Why in the first Pattern.compile("\\\\[")
is the \\\\
OK, but for the second it is redundant? 为什么第一个
Pattern.compile("\\\\[")
是\\\\
OK,但是第二个它是多余的?
The ]
symbol is not a special regex operator outside the character class if there is no corresponding unescaped [
before it. 所述
]
符号不是字符类之外的特殊的正则表达式算子,如果没有相应的未逸出[
之前它。 Only special characters require escaping. 只有特殊字符才需要转义。 A
[
is a special regex operator outside a character class (as it may mark the starting point of a character class). A
[
是字符类之外的特殊正则表达式运算符(因为它可能标记字符类的起点)。 Once the Java regular expression engine sees an unescaped [
in the pattern, it knows there must be a ]
to close the character class ahead. 一旦Java正则表达式引擎看到未转义的
[
在模式中,它知道必须有]
以关闭前面的字符类。 Whether it is escaped or not, it does not matter for the engine. 无论是否逃脱,对发动机都无关紧要。 If there is no opening
[
in the expression, the ]
is treated as a mere literal ]
symbol. 如果没有开头
[
在表达式中,则]
被视为仅仅是文字]
符号。 So, [abc]
will match a
, b
or c
, and \\[abc]
or \\[abc\\]
will match [abc]
literal character sequence. 因此,
[abc]
将匹配a
, b
或c
, \\[abc]
或\\[abc\\]
将匹配[abc]
文字字符序列。
So, the [
should be escaped always, and ]
does not have to be escaped outside a character class . 因此,
[
应该总是被转义,并且]
不必在字符类之外转义。
When used inside a character class , both [
and ]
must be escaped inside a Java regular expression as they may form intersection/subtraction patterns. 在字符类中使用时 ,
[
和]
必须在Java正则表达式中进行转义,因为它们可能形成交集/减法模式。
]
is only considered metacharacter if it is part of [...]
. ]
只考虑元字符,如果它是的一部分[...]
But if your regex doesn't contain unescaped opening [
, which ]
would be closing, such bracket is considered simple literal and doesn't require escaping. 但是,如果你的正则表达式不包含未转义的开放
[
,哪个]
将关闭,这样的括号被认为是简单的文字并且不需要转义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.