Cut C Style Multi-line Comments

I have a C # string object containing generic method code, preceded by some standard C-Style strings.

I decided that I could use System.Text.RegularExpressionsto delete the comment block, but I can make it work.

I tried:

code = Regex.Replace(code,@"/\*.*?\*/","");

Is it possible to point in the right direction?

+3
source share
4 answers

Use the option parameter RegexOptions.Multiline.

string output = Regex.Replace(input, pattern, string.Empty, RegexOptions.Multiline);

Full example

string input = @"this is some stuff right here
    /* blah blah blah 
    blah blah blah 
    blah blah blah */ and this is more stuff
    right here.";

string pattern = @"/[*][\w\d\s]+[*]/";

string output = Regex.Replace(input, pattern, string.Empty, RegexOptions.Multiline);
Console.WriteLine(output);
+2
source

You use backslashes to avoid *in regex, but you also need to escape those backslashes in a C # line.

Thus, @"/\*.*?\*/"or"/\\*.*?\\*/"

, , , .

+3

You need to avoid your backslashes in front of the stars.

string str = "hi /* hello */ hi";
str = Regex.Replace(str, "/\\*.*?\\*/", " ");
//str == "hi  hi"
0
source

You can try:

/\/\*.*?\*\//

Since there are some / in the regex, it's better to use a different delimiter like:

#/\*.*?\*/#
0
source

Source: https://habr.com/ru/post/1738967/


All Articles