Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TextBox2.Text = RemoveCommentBlocks(TextBox1.Text) End Sub
'/// <summary>
'/// Removes "/*...*/" comment blocks (single-line and multi-line) from our string
'/// taking care of possible nested comment blocks. Also calls RemoveInlineComments
'/// which removes "--...." comments.
'/// </summary>
'/// <param name="strText">The string representing the code to remove the comments from</param>
'/// <returns>String</returns>
Private Function RemoveCommentBlocks(ByVal strText As String) As String
'StringBuilder is created for speed.
'The stringbuilder takes the output of RemoveInlineComments which
'removes all "--...." things.
Dim objSB As New StringBuilder(RemoveInlineComments(strText))
'Create a regular expression to match "/* ... */" patterns
Dim objRegEx As _
New Regex("/\*[\d\D]*?\*/", _
RegexOptions.Multiline _
+ RegexOptions.CultureInvariant _
+ RegexOptions.ECMAScript)
'Start parsing our code to find "/* ... */" patterns.
'We may also find "/*.../*...*/" patterns if we have nested comments
'But we will take care of them using FindInner()
Try
'Go on as long as we find matches
While (Not objSB.Equals(String.Empty)) AndAlso objRegEx.IsMatch(objSB.ToString)
'If we find a match check whether it is a "/*.../*...*/" pattern
'using FindInner
Dim strMatch As String = objRegEx.Match(objSB.ToString).ToString
objSB.Replace(FindInner(strMatch, objRegEx), "")
End While
'Spit the result
Return (objSB.ToString)
Catch ex As Exception
'Silly exception handling
MsgBox(ex.Message)
End Try
End Function
'/// <summary>
'/// Removes inline "--..." comments
'/// </summary>
'/// <param name="strText">The string representing the code to remove the comments from</param>
'/// <returns>String</returns>
Private Function RemoveInlineComments(ByVal strText As String) As String
'Create a StringBuilder for speed
Dim objSB As New StringBuilder(strText)
'Create a regular expression to match "--..." patterns
Dim objRegEx As _
New Regex("--.*$", _
RegexOptions.Multiline _
+ RegexOptions.CultureInvariant _
+ RegexOptions.ECMAScript)
Try
'Go on while we have a match
While (Not objSB.Equals(String.Empty)) AndAlso objRegEx.IsMatch(objSB.ToString)
'Take care of the match
Dim strMatch As String = objRegEx.Match(objSB.ToString).ToString
objSB.Replace(strMatch, ControlChars.CrLf)
End While
'Spit result
Return (objSB.ToString)
Catch ex As Exception
'Silly exception handling
MsgBox(ex.Message)
End Try
End Function
'/// <summary>
'/// Finds inner blocks in strings following a "/*.../*...*/" pattern and locates the
'/// innermost block
'/// </summary>
'/// <param name="strMatch">A string that comes from a regular expression match
'/// which follows a "/*.../*...*/" pattern
'/// </param>
'/// <param name="objRegEx">The regular expression that searches for "/*...*/" patterns</param>
'/// <returns>String</returns>
Private Function FindInner(ByVal strMatch As String, ByVal objRegEx As Regex) As String
'Get the position of the second "/*" we may have inside our string
Dim intPos As Int32
intPos = InStr(strMatch.Substring(3, Len(strMatch) - 5), "/*")
If intPos > 1 Then
'If we've got an "/*" inside our string, recursively call
'our function in case we've got more than one levels of nested
'comment blocks.
Return FindInner(objRegEx.Match(strMatch.Substring(intPos, Len(strMatch) - intPos)).ToString, objRegEx)
Else
'We haven't got "/*"s inside our string anymore,
'so return the resulting string. If we initially got a string which
'does not contain an "/*", the function will just return the same string
Return strMatch
End If
End Function