简体   繁体   English

Star - 使用正则表达式在字符串中查找字符 *

[英]Star - look for the character * in a string using regex

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# 正则表达式机制不允许我执行以下操作:

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?显然它认为我的星星代表正则表达式,有没有办法告诉正则表达式机制将星星视为星星而不是其他任何东西?

你需要用反斜杠转义星号: @"\\*"

* in Regex means: *在正则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 .例如, \\*\\x2A

[ character_group ]

Matches any single character in character_group .匹配 character_group 中的任何单个character_group

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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