I have the following example code:
var inputString = "ñaáme";
inputString = inputString.Replace('ñ', '\u00F1');
var normalizedString = inputString.Normalize(NormalizationForm.FormD);
var result = Regex.Replace(normalizedString, @"[^ñÑa-zA-Z0-9\s]*", string.Empty);
return result.Replace('\u00F1', 'ñ'); // naame :(
I need to normalize the text without removing the "ñ"s
I followed this example But it's for Java and it has not worked for me
I want your result to be: "ñaame".
You may match any Unicode letter other than your specific letter ñ
and ASCII letters (that do not need normalization) with (?i)[\p{L}-[ña-z]]+
regex and normalize it. Then, also remove any combining marks from the string.
Use
var inputString = "ñaáme";
var result = string.Concat(Regex.Replace(inputString, @"(?i)[\p{L}-[ña-z]]+", m =>
m.Value.Normalize(NormalizationForm.FormD)
)
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark));
Console.Write(result);
See the C# demo
Pattern description
(?i)
- ignore case modifier[
- start of a character class
\p{L}
- any Unicode letter-[
- other than
ña-z
- ñ
and ASCII letters]
- end of the subtraction class]+
- 1 or more occurrences.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With