使用RegEx查找VBA注释 [英] Finding VBA Comments using RegEx

查看:75
本文介绍了使用RegEx查找VBA注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式查找所有VBA注释。我有一些可以正常工作的东西,但是有一些例外我无法弄清楚。

I am trying to find all VBA comments using regular expressions. I have something that mostly works, but there are a few exceptions that I cannot figure out.

我正在使用的表达式:

'(?!.*").*

接受我们的测试代码:

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

Here is a link to this example in RegExr: http://regexr.com/3f24h

出于某些原因,第三个和第四个示例未捕获。问题似乎出在字符串中评论,但我不知道如何解决。

For some reason that third and fourth examples are not capturing. The problem seems to be with having a string value in the comments and I cannot figure out how to fix it.

任何advic e?

推荐答案

也许类似

^(?:[^"'\n]*("(?:[^"\n]|"")*"))*[^"]*'(.*)$

它处理多个带引号的字符串,以及带引号(双)的字符串 (我相信这是VBA的方式)。

It handles multiple quoted strings, as well as strings having quoted (double) "'s (which I believe is VBA's way).

(我保证在某些情况下会失败,但可能在大多数情况下都可以使用;)

(I guarantee it will fail in some cases, but probably will work in most ;)

检查在regex101上显示出来。

Check it out here at regex101.

编辑

添加了一些内容的例子,并调整了正则表达式。但是,它仍然不能处理放在括号中的标识符(我什至不知道这是什么意思:S参见最后一行)。但是,现在它处理了他继续的行注释。

Added some of Comintern's examples and adjusted the regex. It still can't handle the bracketed identifiers though (which I don't even know what it means :S See the last line). But it now handles his continued line comments.

^(?:[^"'\n]*(?:"(?:[^"\n]|"")*"))*[^']*('(?:_\n|.)*)

在regex101处查看

这篇关于使用RegEx查找VBA注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆