Hello (e-mail address removed),
I'm trying to obtain all lines in a C# source file. So a statement
spanning multiple lines would return one match.
For instance:
var i =
1;
Is one match.
For a given C# source file, I want the same file without any multiline
statements.
Can someone point me in the right direction?
There is an easy answer and a nasty answer to this question...
first... not all lines end with a ; and you'd probably don't want all lines
to be compressed
eg
namespace xyz
{
class zyx
{
}
}
probably shouldn't become:
namespace xyz{ class zyx { }}
which would be perfectly legal.
To add to the problem, you can have comments, and multiline comments containing
source like lines:
/*
var x =
new var();
*/
or you could have mutltiple nested lines and a verbatim string:
string xyz = @"this
is
a test
";
which would actually read:
string xyz = "this\r\nis\r\na test\r\n";
and you could make it even worse if you add mutiple strings (some containing

with embedded escaped " in there and pieces that could look like comments
like this:
string @"
xyz /* this
is
a
test */ // now this is fun; " /* another comment */ ; // end of line comment
Or you could have compiler directives... messing things up even further...
Your end conclusion here should be that regex is not flexibel enough to catch
all these possible issues, and that if you really want to accomplish this
you need to look elsewhere...
You basically have a few options though:
1) lexer/parser and a lot of work. You could write a source parser using
these tools and make sure you handle all the intricacies found in .NET
2) Compile/decompile using for example Reflector.NET (see red-gate.com)
3) Coderush or Resharper, they are both add-ins to Visual Studio and allow
you to manupulate code files in a DOM-tree like structure. Which in turn
saves you the work of writing your own lexer/parser. The best part is that
teh underlying engine for Coderush is free and that you can download it from
the DevExpress.com website along with a load of samples on how to do this.
They also have a great forum/newsgroups where you could ask for pointers
on how to do this, they migth even have most of it already implemented, as
they have code-beautyfying options.
4) just forget about it