Finding VBA Comments using RegEx

前端 未结 3 1472
予麋鹿
予麋鹿 2021-01-23 22:49

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.

Expression I

3条回答
  •  醉酒成梦
    2021-01-23 23:33

    Maybe something like

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

    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 ;)

    Check it out here at regex101.

    Edit

    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|.)*)
    

    Check it out here at regex101.

提交回复
热议问题