问题描述
.*
和 [.]*
之间的确切区别是什么?
What is the exact difference between .*
and [.]*
?
我试图将这些放在括号内,以供反向引用,但看到结果不一样,虽然我不明白为什么.
I have tried to put these inside parentheses, for back-reference, and saw the results are not the same although I don't understand why.
.
应该匹配任何单个字符.
The .
is supposed to match any single character.
所以我猜它是否在方括号内对于 *
(匹配零个或多个)运算符不应该很重要.但它是.为什么?
So I guess whether it's inside square brackets or not should not be important with the *
(match zero or more) operator. But it is. Why?
推荐答案
在 .*
中,点是匹配除换行符以外的任何字符的特殊字符(如果指定DOTALL 修饰符).在字符类 ([.]
) 中,点失去其特殊含义并开始匹配文字点.
In .*
, a dot is a special character matching any character but a newline (it will match all characters if you specify a DOTALL modifier). In a character class ([.]
), a dot loses its special meaning and starts matching a literal dot.
这是所有正则表达式风格的通用行为.
It is a universal behavior across all regex flavors.
这篇关于正则表达式点是否带方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!