【发布时间】:2017-05-27 16:32:39
【问题描述】:
我正在尝试使用正则表达式查找所有 VBA cmets。我有一些最有效的方法,但也有一些我无法弄清楚的例外情况。
我正在使用的表达式:
'(?!.*").*
获取我们的测试代码:
Working - This is a test 'This should be captured
Working - "this is a test" 'This should be captured
Not Working - "this is a test" 'This should be "captured"
Not Working - This is a test 'This should be "captured"
Working - "this is a test 'this should not capture'" 'this should capture
Working - "this isn't a test" 'this should capture
这是 RegExr 中此示例的链接:http://regexr.com/3f24h
由于某些原因,第三个和第四个示例没有捕获。问题似乎在于 cmets 中有一个字符串值,我不知道如何解决它。
有什么建议吗?
【问题讨论】:
-
第三个包含双引号和
'之后的子字符串,由于(?!.*")前瞻条件,不能有双引号。 -
试试
'(?!\*\*)(?!\* )[^']*$和regexp.Multiline=True。 -
在这里试一试regex101.com
-
这行得通,但在最后一行变得很奇怪,它变得流氓并抓住了一切。 regexr.com/3f24h
-
没错。实际上,上下文可能要复杂得多,通常需要一个解析器来获取这些 cmets。