Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse VB code for comments

I'making a small NodeJS app that can run against a large codebase to detect certain words within the code comments. I'm having trouble reliably matching the comment syntax for *.vb files that use a single quote ' to being a comment.

Here's the regex I have so far, which is imperfect: ^(?:[^"]+)'(.+)$

Test page: https://regex101.com/r/V0CHV2/4

The tricky part is not matching single quotes within a string. Here's my test case.

''' <summary>
''' Runs the auth check.
''' </summary>
''' <param name="authToken">The auth token.</param>
Public Shared Sub AuthCheck(ByVal authToken As AuthToken)
  'This is a "comment" - oh yeah
  If Not authToken.Equals(foo, StringComparison.OrdinalIgnoreCase) Then 'else
    Throw New Exception("don't count this as a comment!") 'comment here
  End If
End Sub
like image 601
FiniteLooper Avatar asked Jul 13 '26 01:07

FiniteLooper


2 Answers

I came up with this variant:

^(?:(?:"(?:\\"|[^"])*")|[^"'])*\s*'+\s*(.*)

Note that writing parsers for this sort of stuff using purely regular expressions can be difficult. This seems to extract the content properly.

This accounts for situations like this:

func("This is \"Bob's\" string") ''' Comment!

It's important to not get tripped up on things like \" where that's actually not the end of the string.

like image 183
tadman Avatar answered Jul 14 '26 15:07

tadman


I would give a shot with \s*'[^"\n]*("[^"\n]*"[^"\n]*)*$. You can see the regex on regex101.com with your sample

like image 32
Thomas Ayoub Avatar answered Jul 14 '26 14:07

Thomas Ayoub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!