Hello Chris,
Thanks very much for sharing it in the community.
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
!From: "Chris Langsenkamp" <
[email protected]>
!References: <
[email protected]> <
[email protected]> <eCF0N
[email protected]>
!Subject: Re: Regular expression to detect comments REVISED
!Date: Tue, 19 Aug 2003 20:58:18 -0500
!Lines: 376
!MIME-Version: 1.0
!Content-Type: multipart/alternative;
! boundary="----=_NextPart_000_038C_01C36694.9DBC4460"
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <
[email protected]>
!Newsgroups: microsoft.public.dotnet.framework
!NNTP-Posting-Host: dsl1a-281.ccrtc.com 209.132.165.27
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:51660
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!well gee I guess I shouldn't write code into a message and not check it for accuracy (duh).
!Below is the correct code, illustrating my test cases and finally the output...still not regex, but at least this works and will help
somebody work out the regex logic?
!Chris Langsenkamp
!--------------------------------------------------------------------------------
!Module Module1
! Sub Main()
! ' normal comment line
! Console.WriteLine(" " & FindComment("'This is a test comment"))
! ' normal comment line with a single quote in it
! Console.WriteLine(" " & FindComment("'This is a " & Chr(34) & "test comment"))
! ' code line ending in a comment
! Console.WriteLine(" " & FindComment("x=1 'This is a test comment"))
! ' code line ending in a comment containing an apostrophe
! Console.WriteLine(" " & FindComment("x=1 'This is a test 'comment"))
! ' code line ending in a comment containing a double quote
! Console.WriteLine(" " & FindComment("x=1 'This is a test " & Chr(34) & "comment"))
! ' code line ending in a comment containing two double quotes
! Console.WriteLine(" " & FindComment("x=1 'This is a test " & Chr(34) & "comment" & Chr(34)))
! ' code line containing two double quotes followed by comment containing an apostrophe
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "1" & Chr(34) & " 'This is a test ' comment"))
! ' code line containing two double quotes containing an apostrophe followed by comment
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "they're" & Chr(34) & " 'This is a test comment"))
! ' code line containing two double quotes containing an apostrophe followed by comment containing an apostrophe
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "they're" & Chr(34) & " 'This is a test ' comment"))
! ' code line containing two double quotes containing an apostrophe followed by comment containing a double quote
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "they're" & Chr(34) & " 'This is a test " & Chr(34) & "comment"))
! ' code line containing two double quotes containing an apostrophe followed by comment containing two double quotes
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "they're" & Chr(34) & " 'This is a test " & Chr(34) & "comment" & Chr
(34)))
! ' code line containing two double quotes containing an apostrophe followed by comment containing two double quotes
containing an apostrophe
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "they're" & Chr(34) & " 'This is a test " & Chr(34) & "comm'nt" & Chr
(34)))
! ' code line containing two double quotes containing an apostrophe followed by comment containing two double quotes
containing an apostrophe followed by an apostrophe
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "they're" & Chr(34) & " 'This is a test " & Chr(34) & "comm'nt" & Chr
(34) & "'"))
! ' code line containing two double quotes containing an apostrophe followed by comment containing an apostrophe and
containing two double quotes containing an apostrophe followed by an apostrophe
! Console.WriteLine(" " & FindComment("x=" & Chr(34) & "they're" & Chr(34) & " 'This is a test '" & Chr(34) & "comm'nt" & Chr
(34) & "'"))
! End Sub
! Function FindComment(ByVal ThisLine As String) As String
! Dim LineArray() As String
! Dim ThisComment As String = ""
! Dim a As Byte = InStrRev(ThisLine, "'") ' last apostrophe
! Console.WriteLine(ThisLine)
! Do While a > 0
! If InStr(Mid(ThisLine, a), Chr(34)) = 0 Then
! ThisComment = a.ToString & ": " & Mid(ThisLine, a) 'found comment at end of line and has no double quotes in it
! Else
! ' might be a comment with one or more double-quotes in it - let's find out
! If InStr(Mid(ThisLine, 1, a), Chr(34)) = 0 Then
! ThisComment = a.ToString & ": " & Mid(ThisLine, a) 'found comment at end of line and no matching double quote
before apostrophe
! Else
! ' there's at least one double quote preceeding...let's see if there are all matched pairs preceeding
! LineArray = Split(Mid(ThisLine, 1, a), Chr(34))
! If LineArray.Length Mod 2 <> 0 Then
! ThisComment = a.ToString & ": " & Mid(ThisLine, a) 'found comment at end of line
! ' all double quotes preceeding are matched pairs because the split returns an odd number of elements
! End If
! End If
! End If
! If a > 1 Then a = InStrRev(ThisLine, "'", a - 1) Else Exit Do
! Loop
! If ThisComment <> "" Then
! Return ThisComment
! Else
! Return "No Comment Found"
! End If
! End Function
!End Module
!--------------------------------------------------------------------------------
!Console Output:
!'This is a test comment
! 1: 'This is a test comment
!'This is a "test comment
! 1: 'This is a "test comment
!x=1 'This is a test comment
! 5: 'This is a test comment
!x=1 'This is a test 'comment
! 5: 'This is a test 'comment
!x=1 'This is a test "comment
! 5: 'This is a test "comment
!x=1 'This is a test "comment"
! 5: 'This is a test "comment"
!x="1" 'This is a test ' comment
! 7: 'This is a test ' comment
!x="they're" 'This is a test comment
! 13: 'This is a test comment
!x="they're" 'This is a test ' comment
! 13: 'This is a test ' comment
!x="they're" 'This is a test "comment
! 13: 'This is a test "comment
!x="they're" 'This is a test "comment"
! 13: 'This is a test "comment"
!x="they're" 'This is a test "comm'nt"
! 13: 'This is a test "comm'nt"
!x="they're" 'This is a test "comm'nt"'
! 13: 'This is a test "comm'nt"'
!x="they're" 'This is a test '"comm'nt"'
! 13: 'This is a test '"comm'nt"'
!--------------------------------------------------------------------------------
!Hi all,
!I need some help from a RegEx guru: Can some kind soul come up with a regex expression that detects comments in VB
source lines? I think that this should be something that detects an apostrophe which is not enclosed within quotes.
!Thanks!
!