解释查找CSS注释的正则表达式 [英] Explain regex that finds CSS comments
问题描述
我发现这个正则表达式代码会在w3.org的 CSS语法中找到注释< a> page。
I found this regex code that finds comments in w3.org's CSS grammar page.
\/\*[^*]*\*+([^/*][^*]*\*+)*\/
这是很长,有点难以理解。我只是把
It is quite long and bit difficult to understand. I'd just put
\/\*.*\*\/
来查找注释,但是当我在 RegexPal 找到单行注释,而不是多行注释,而原始的正则表达式可以找到所有类型的注释。
to find comments, but when I tested it in RegexPal it finds single line comments and not multi-line comments whereas the original regex can find all types of comments.
我不明白
+([^/*][^*]*\*+)*
推荐答案
你发现只有单行注释的原因是,在典型的正则表达式中,。
匹配任何(换行除外);而另一个使用一个否定的字符类,它匹配任何除了指定的字符,因此可以匹配换行符。
The reason yours finds only single line comments is that, in typical regular expressions, .
matches anything except newlines; whereas the other one uses a negated character class which matches anything but the specified characters, and so can match newlines.
但是,如果你要解决这个问题选项为多行或仿佛单行匹配),您会发现它将从第一个注释的 / *
匹配到 * /
的最后评论;您必须使用非贪心量词,。*?
才能匹配不超过一个注释。
However, if you were to fix that (there's usually an option for multiline or "as if single line" matching), you would find that it would match from the /*
of the first comment to the */
of the last comment; you would have to use a non-greedy quantifier, .*?
, to match no more than one comment.
然而,你给出的更复杂的正则表达式比这更复杂。基于nikc.org的答案,我相信它是强制执行评论不能嵌套的限制;也就是说,它们中不能包含 / *
。在允许注释 / * like / * this * /
(即,内部/ *既不被禁止也不是嵌套注释)的其他语言中,模式 \ / \ *。*?\ * \ /
将适合匹配它们。
However, the more complex regular expression you give is even more complex than that. Based on nikc.org's answer, I believe it is to enforce the restriction that "comments may not be nested"; that is, they must not contain /*
within them. In other languages which permit comments /* like /* this */
(that is, an internal /* is neither prohibited nor a nested comment), the pattern \/\*.*?\*\/
would be appropriate to match them.
这篇关于解释查找CSS注释的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!