C# Star - 使用正则表达式在字符串中查找字符 *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13107860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Star - look for the character * in a string using regex
提问by user1322801
I am trying to find the following text in my string : '***'the thing is that the C# Regex mechanism doesnt allow me to do the following:
我试图在我的字符串中找到以下文本:'***'问题是 C# Regex 机制不允许我执行以下操作:
new Regex("***", RegexOptions.CultureInvariant | RegexOptions.Compiled);
due to
由于
ArgumentException: "parsing "*" - Quantifier {x,y} following nothing."
ArgumentException:“解析“ *” - 量词 {x,y} 不跟任何东西。”
obviously it thinks that my stars represents regular expressions, is there a way to tell the Regex mechanism to treat stars as just stars and nothing else?
显然它认为我的星星代表正则表达式,有没有办法告诉正则表达式机制将星星视为星星而不是其他任何东西?
采纳答案by SLaks
You need to escapethe star with a backslash: @"\*"
你需要用反斜杠转义星星:@"\*"
回答by Ria
*in Regexmeans:
*在Regex手段:
Matches the previous element zero or more times.
匹配前一个元素零次或多次。
so that, you need to use \*or [*]instead.
因此,您需要使用\*或[*]代替。
explain:
解释:
\When followed by a character that is not recognized as an escaped character in this and other tables in this topic, matches that character. For example,
\*is the same as\x2A.
[ character_group ]Matches any single character in
character_group.
\当后跟在本主题的此表和其他表中未被识别为转义字符的字符时,匹配该字符。例如,
\*与 相同\x2A。
[ character_group ]匹配
character_group.

