正则表达式源代码注释 [英] Regex to source code comments
问题描述
是否有正则表达式匹配任何行注释,但避免字符串中的注释?
我需要 //
(包括 //
)之后的所有内容
Is there a regular expression to match any line comment, but avoiding the comments inside strings?
I need all content in a line after //
(with the //
included)
例如:
//Comment (match!)
bla bla bla bla //Comment (match!)
this string "foo // foo" (don't match because it's inside "")
推荐答案
这里是另一个解决方案,应该捕获每一个单行注释(看到它工作在 regex101 ):
Here's another solution that should catch every single-line comment (see it work on regex101):
(\ / \ /。 *)|(?:\\|。)*?
所有注释将在第一个匹配
All the comments will be captured in the first match group.
它可以在任何正则表达式中使用惰性量词,这几乎是所有的。我使用的技术是匹配引用的字符串,删除从可用的文本匹配我们想要的:评论。这个技术在RexEgg.com上详细解释为 The Greatest Regex Trick Ever 。
It will work in any regex flavor that has lazy quantifiers, which is almost all of them. The technique I used is to match quoted strings specifically so they get "removed" from the text available to match what we want: comments. This technique is explained in detail on RexEgg.com as The Greatest Regex Trick Ever.
细分:
(\ / \ /.*)
匹配注释并捕获组
:\\|。)*?
匹配引用字符串,避免
"(?:\\"|.)*?"
matches quoted strings, avoiding any escaped quotes inside
- 内部非捕获组
(?: \\|。)
匹配转义的引号或下一个字符,成功地跨越转义引号,而不是它们匹配作为真实报价 - 整个替换具有
*?
惰性量词,所以它命中下一个而不是转到另一个带引号的字符串。
- The inside non-capturing group
(?:\\"|.)
matches an escaped quote OR the next character, successfully passing right over the escaped quotes rather than having them match as a "real" quote - The whole alternation has the
*?
lazy quantifier so it hits the next "real" quote, rather than proceeding to another quoted string.
这篇关于正则表达式源代码注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!