EricEric, do you want a Regex thatcan be used from the VS Find/Replace feature, or are you trying to write a program to do this? Also, do you only want to remove comments at the end of program lines (in other words, do you want to retain comments that are alone on a line or preceded only with whitespace?). As for the location where the comment starts, that can only be done with a program based Regex.
If you want a VS Find/Replace Regex that can find all comments in VB, then John's pattern is close, but I would change it to this...
(^|:b+)'.+
... otherwise, it misses several lines (those lines that begin with a comment, and comments that like '----- and '<summary> and even an empty comment.
But, if you want to use a program and obtain the indexes to the start of the comment, you will need Eping Wang's pattern. You would use...
Dim mx as Match = reg.Matches(vbString)
... to get the comment match. Then mx.Index and mx.Length can be used forworking withthe match in your string.
Lastly, Eping Wang correctly points out his pattern does not properly match comments that contain double quotes. To avoid the problem, one must only match comments when not inside of adouble quoted string. Here is an example in C# using Groups and pairing to only select a single quote to the end of line when the single quote appears outside of a string...
string pattern = @"
(
""(?(quote)(?<-quote>)|(?<quote>))
| [^""']
| (?(quote)('))
)*
(?(quote)(?!))(?<comment>'.*)
";
string test = @"
Dim A As String ' This is a Dim statement.
A = ""'"" & 500 * 6 & ""'""
If A <> ""''"" Then ' We should never get here!!!!!
MsgBox(""See. We Tried!"") ' what does he mean 'we'?
End If
Dim b As String = ""How About this"" 'here's a comment
Dim c as string = ""another"" ' this ""comment"" has double quotes...
";
foreach (Match m in Regex.Matches(test, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline))
{
Group mx = m.Groups["comment"];
Console.WriteLine("{0}, {1}: {2}", mx.Index, mx.Length, mx.Value);
}
Les Potter, Xalnix Corporation,
Yet Another C# Blog