我在vb.net中有一个语法高亮功能.我使用正则表达式匹配"!IF",然后将其着色为蓝色.这很有效,直到我试图弄清楚如何做评论.
我正在写这篇评论的语言可以是如果该行以单引号开头'或者如果该行中的任何地方有两个单引号
'this line is a comment
!if StackOverflow = "AWESOME" ''this is also a comment
Run Code Online (Sandbox Code Playgroud)
现在我知道如何查看它是否以单行开头^'但我需要将字符串一直返回到行尾,这样我就可以将整个注释的颜色设置为绿色,而不仅仅是单引号.
您不应该需要代码,但这里是一个片段,以防它有所帮助.
For Each pass In frmColors.lbRegExps.Items
RegExp = System.Text.RegularExpressions.Regex.Matches(LCase(rtbMain.Text), LCase(pass))
For Each RegExpMatch In RegExp
rtbMain.Select(RegExpMatch.Index, RegExpMatch.Length)
rtbMain.SelectionColor = ColorTranslator.FromHtml(frmColors.lbHexColors.Items(PassNumber))
Next
PassNumber += 1
Next
Run Code Online (Sandbox Code Playgroud) 我试图使用正则表达式查找所有VBA注释.我有一些主要有用的东西,但有一些我无法弄清楚的例外.
我正在使用的表达式:
'(?!.*").*
Run Code Online (Sandbox Code Playgroud)
拿我们的测试代码:
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
Run Code Online (Sandbox Code Playgroud)
以下是RegExr中此示例的链接:http: //regexr.com/3f24h
由于某种原因,第三和第四个例子没有捕获.问题似乎是在评论中有一个字符串值,我无法弄清楚如何解决它.
有什么建议?