I need to change every multi-line comments that looks like this:
/*
Some
multiline
comment
*/
,into this:
#
#
#
#
#
The number of rows must be the same. How can I achieve this in one expression using Regex from .NET?
I need to change every multi-line comments that looks like this:
/*
Some
multiline
comment
*/
,into this:
#
#
#
#
#
The number of rows must be the same. How can I achieve this in one expression using Regex from .NET?
Assuming you do not have comment-like strings in string literals and that comments can't be nested (since the string comes from T-SQL), you can try
var rx = new Regex(@"/\*(?s:.*?)\*/");
var txt = @"/*
Some
multiline
comment
*/";
var replaced = rx.Replace(txt, m => String.Concat(Enumerable.Repeat("#\r\n", m.Value.Split(new string[] {"\r\n"}, StringSplitOptions.None).Count())).Trim());
Result:
The /\*(?s:.*?)\*/
regex matches any text between /*
and */
. The logic is that we get the whole match, split it with linebreaks, and then build a replacement string based on the number of lines.
If you want to match just the lines that are all-comments, you can use the following regex (see demo):
(?m)^\s*/\*(?s:.*?)\*/\s*$