如果我有几个字符串:
str = "This is a cool string (orch. details here): 2. Denied."
str2 = " (Orch. details here)" <--notice the capital letter
然后我执行这行代码以尝试清除该部分:
str3 = Regex.Replace(str, str2, "", RegexOptions.IgnoreCase);
str3
最终与执行前的 str
完全相同。我怀疑它没有“看到”其他字符,例如句点,因为当我只使用 str
和 str2
中的简单字母字符执行此操作时,它将仅替换它很好。
什么给了?! :)
感谢您的任何见解!
最佳答案
括号和句点在正则表达式中都有含义。因此,它不是试图逐字匹配 str2
的内容,而是尝试匹配 str2
定义的正则表达式。如果你想让它按字面意思匹配,你需要转义字符串
str2 = Regex.Escape(str2);
关于c# - Regex.Replace 无法按预期使用 Regexoptions.IgnoreCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974333/