简体   繁体   中英

C# multiline comment regex not working

I'm new to regex and i'm trying to make a regex that find all C-style block comments (/* and */)

So I search the web and find a good one who works on RegExr.com!

/\/\*[^]*?\*\//g

But when I want to put this regex in my C# code, it throws me an error.

System.Text.RegularExpressions.Regex _reg = new System.Text.RegularExpressions.Regex(@"/\/\*[^]*?\*\//g");

Can you help me finding my error ? Thanks!

You need to remove the regex delimiters, and use singleline mode with . (either inline (?s) or RegexOptions.Singleline flag) to match any character including a newline:

(?s)/\*.*?\*/

See demo

In your regex, you have [^] that matches any symbol including a newline that is only working in JavaScript. And /.../[modifier] is not necessary in C# since flags can be specified as optional parameters in regex class methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM