使用不区分大小写的正则表达式替换时需要注意哪些事项?

Pri*_*lia 2 c# regex replace case-insensitive

我编写了以下代码来在C#中进行不区分大小写的替换:

Regex.Replace(textBoxText, 
    Regex.Escape(findText), 
    replaceText, 
    RegexOptions.IgnoreCase);
Run Code Online (Sandbox Code Playgroud)

只是想检查,这是否是正确的方法,还是有更好的方法,我是否忽略了一些我应该更好地意识到的东西.

注意:请不要向我提供一些手工制作的代码,我使用了codeproject中的快速替换功能,并且该代码在客户端崩溃,我无法知道用户使用了什么输入.所以,我更喜欢一些简单但正确可靠的方法.

Abe*_*bel 7

您的代码似乎没问题,但请记住,当您执行不区分大小写的匹配时,您可以使用当前的区域设置或区域性.最好添加您想要的文化,或让用户选择它.CultureInvariant通常是在任何语言环境中采取相同行为的一般选择:

Regex.Replace(textBoxText, 
    Regex.Escape(findText), 
    replaceText, 
    RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
Run Code Online (Sandbox Code Playgroud)

要使用其他语言环境,您需要做更多的hocus pocus:

// remember current
CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;

// set user-selected culture here (in place of "en-US")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

// do the regex
Regex.Replace(textBoxText, 
    Regex.Escape(findText), 
    replaceText, 
    RegexOptions.IgnoreCase);

// reset the original culture
Thread.CurrentThread.CurrentCulture = originalCulture;
Run Code Online (Sandbox Code Playgroud)

请注意,您可以打开或关闭不区分大小写.它不是切换,这意味着:

// these three statements are equivalent and yield the same results:
Regex.Replace("tExT", "[a-z]", "", RegexOptions.IgnoreCase);
Regex.Replace("tExT", "(?i)[a-z]", "", RegexOptions.IgnoreCase);
Regex.Replace("tExT", "(?i)[a-z]", "");

// once IgnoreCase is used, this switches it off for the whole expression...
Regex.Replace("tExT", "(?-i)[a-z]", "", RegexOptions.IgnoreCase);

//...and this can switch it off for only a part of the expression:
Regex.Replace("tExT", "(?:(?-i)[a-z])", "", RegexOptions.IgnoreCase);
Run Code Online (Sandbox Code Playgroud)

最后一个很有意思:在(?:)非捕获分组括号之后,案例切换(?-i)不再有效.您可以在表达式中随意使用它.在没有分组的情况下使用它会使它们有效,直到下一个区分大小写切换或结束.

更新:我做了错误的假设,你不能进行区分大小写切换.编辑上面的文本时考虑到了这一点.