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
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.
I would give a shot with \s*'[^"\n]*("[^"\n]*"[^"\n]*)*$. You can see the regex on regex101.com with your sample
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With