【问题标题】:How match string ignoring case and accent? [duplicate]如何匹配字符串忽略大小写和重音? [复制]
【发布时间】:2019-08-29 16:05:15
【问题描述】:

我在 Unity 中使用正则表达式匹配字符串忽略大小写。

我的代码:

...
string _word = "Esto Es Una Cadena Con Texto"
string final = "esto es una cadena con texto"

if(Regex.IsMatch(final, Regex.Escape(_word), RegexOptions.IgnoreCase)){
//
}
...

据此:

string _word = "Estó Es Uná Cadená Cón Textó"
string final = "esto es una cadena con texto"

是否有代码匹配前面的字符串忽略大小写和重音?

【问题讨论】:

标签: c# regex


【解决方案1】:

尝试使用 CultureInfo:

string _word = "Estó Es Uná Cadená Cón Textó";
string final = "esto es una cadena con texto";
var compareInfo = CultureInfo.InvariantCulture.CompareInfo;
var equal = Convert.ToBoolean(compareInfo.Compare(_word, final));

if (equal)
{
    Console.WriteLine("Hello World!");
}

【讨论】:

    【解决方案2】:

    如果你想匹配这个特定的句子,你可以使用这样的东西:

    string final = "est[oó] es un[aá] caden[aá] c[oó]n text[oó]"
    

    但如果只是想“忽略”重音,我建议您使用 String.replace(char, char)

    string _word = "Estó Es Uná Cadená Cón Textó"
    string final = "esto es una cadena con texto"
    
    _word = _word.replace('é', 'e');  // same for á and ó
    
    if(Regex.IsMatch(final, Regex.Escape(_word), RegexOptions.IgnoreCase)){
    //
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 2011-09-28
      • 1970-01-01
      • 2019-02-06
      • 2021-05-24
      • 1970-01-01
      • 2012-12-10
      • 2012-01-21
      相关资源
      最近更新 更多