Search code examples
c#regexlocalizationcultureinfo

Regex - how to match 'ß' to 'ss' and vice versa


Folks, how can I do a regex match on a sharp-s character (ß) with ss, and vice versa? I tried the invariant culture, but it doesn't return a match. However, if I use String.IndexOf() with the invariant culture, it is able to.

Console.WriteLine("abcßßdefßßghi".IndexOf("ssss", StringComparison.InvariantCultureIgnoreCase) >=0);

var matches = Regex.Matches("abcßßdefßßghi", "ssss", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
Console.WriteLine(matches.Count);

// OUTPUT
True
0

I need to find all of the indices where the match starts. The idea is to highlight the 'matched' string. I'm looking for a clean approach with Regex.Match(), which is preferable to iterating over the string and substring and so on. TIA.


Solution

  • to get over this scenario, I ended up replacing ß|ss with (ß|ss) in the search term and then doing a lookup for it

    var cleanSearchTerm = Regex.Replace(Regex.Escape(RemoveDiacritics(searchTerm)), "ß|ss", "(ß|ss)", RegexOptions.IgnoreCase);
    var matches = Regex.Matches(RemoveDiacritics(item), cleanSearchTerm, RegexOptions.IgnoreCase );
    

    this would give matches for ß and ss in a search term containing ß and/or ss