1

Im using Regex to match string ignoring case in Unity.

My code:

...
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)){
//
}
...

According this:

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

Is there a code to match previous string ignoring case and accent?

Alex Myers
  • 6,196
  • 7
  • 23
  • 39

2 Answers2

1

Try using 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!");
}
0

If you want to match this specific sentence you could use something like this:

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

but if just want to "ignore" accents i would recommend you use 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)){
//
}
Painkiller
  • 115
  • 9