Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

c# - Regex accent insensitive?

I need a Regex in a C# program.


I've to capture a name of a file with a specific structure.

I used the w char class, but the problem is that this class doesn't match any accented char.

Then how to do this? I just don't want to put the most used accented letter in my pattern because we can theoretically put every accent on every letter.

So I though there is maybe a syntax, to say we want a case insensitive(or a class which takes in account accent), or a Regex option which allows me to be case insensitive.

Do you know something like this?

Thank you very much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could simply replace diacritics with alphabetic (near-)equivalences, and then use use your current regex.

See for example:

How do I remove diacritics (accents) from a string in .NET?

static string RemoveDiacritics(string input)
{
    string normalized = input.Normalize(NormalizationForm.FormD);
    var builder = new StringBuilder();

    foreach (char ch in normalized)
    {
        if (CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark)
        {
            builder.Append(ch);
        }
    }

    return builder.ToString().Normalize(NormalizationForm.FormC);
}

string s1 = "Renato Nú?ez David DeJesús Edwin Encarnación";
string s2 = RemoveDiacritics(s1);
// s2 = "Renato Nunez David DeJesus Edwin Encarnacion"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...