[英]Regex.Replace not working as expected with Regexoptions.IgnoreCase
If I have a few strings: 如果我有几个字符串:
str = "This is a cool string (orch. details here): 2. Denied."
str2 = " (Orch. details here)" <--notice the capital letter
and then I do this line of code to try to clear that part out: 然后执行以下代码行以尝试清除该部分:
str3 = Regex.Replace(str, str2, "", RegexOptions.IgnoreCase);
str3
ends up being exactly as what str
was before execution. str3
最终与执行前的str
完全相同。 I suspect that it is not "seeing" the other characters such as the periods because when I do this with just simple alphabetic characters in str
and str2
then it will replace it just fine. 我怀疑它没有“看到”其他字符,例如句点,因为当我在str
和str2
仅使用简单的字母字符来执行此操作时,它将很好地替换它。
What gives?! 是什么赋予了?! :) :)
Thank you for any insight! 感谢您的见识!
Both parens and periods have meanings in regular expressions. 括号和句点在正则表达式中都有含义。 Hence it's not trying to literally match the contents of str2
but instead a regular expression that is defined by str2
. 因此,它不是要在字面上匹配str2
的内容,而是尝试由str2
定义的正则表达式。 If you want it to match literally you need to escape the string 如果要从字面上进行匹配,则需要对字符串进行转义
str2 = Regex.Escape(str2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.