[英]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 */) 我是regex的新手,我正在尝试制作一个能找到所有C样式块注释(/ *和* /)的正则表达式
So I search the web and find a good one who works on RegExr.com! 因此,我在网上搜索并找到了一个在RegExr.com上工作的好人!
/\/\*[^]*?\*\//g
But when I want to put this regex in my C# code, it throws me an error. 但是,当我想将此正则表达式放入我的C#代码中时,会引发错误。
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)
或RegexOptions.Singleline
标志)以匹配包括换行符在内的任何字符:
(?s)/\*.*?\*/
In your regex, you have [^]
that matches any symbol including a newline that is only working in JavaScript. 在正则表达式中,您有[^]
匹配任何符号,包括仅在JavaScript中工作的换行符。 And /.../[modifier]
is not necessary in C# since flags can be specified as optional parameters in regex class methods. 在C#中,不需要/.../[modifier]
,因为可以在正则表达式类方法中将标志指定为可选参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.