正则表达式中的Unicode字符 [英] Unicode characters in Regex

查看:94
本文介绍了正则表达式中的Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式:

return Regex.IsMatch(_customer.FirstName, @"^[A-Za-z][A-Za-z0-9@#%&\'\-\s\.\,*]*$");

现在,有些客户的姓氏或名字上有一个元音字样,如下所示: 布伦丹

Now, some of the customers have a fada over a vowel in their surname or firstname like the following: Brendán

请注意按住 alt 可获得的a上的fada , ctrl ,然后按 a

Note the fada over the a which you can get by holding down alt, ctrl and then pressing a.

我尝试将这些字符添加到正则表达式中,但得到一个

I have tried adding these characters into the regular expression but I get an error when the program tries to compile.

我可以允许用户使用aa fada输入此类字符的唯一方法是完全删除正则表达式,这意味着用户可以输入他们想要的任何内容。

The only way I can allow the user to enter such a character with a a fada is to remove the regular expression completely which means the user can enter anything they want.

是否可以使用上述表达式并以某种方式允许以下字符?

Is there any way to use the above expression and somehow allow the following characters?

á
é
í
ó
ú


推荐答案

仅供参考,您无需在上面的',。中转义。您的角色类 [] ,您可以避免不得不转义通过将-放在字符类的开头或结尾来破折号。

Just for reference you don't need to escape the above ',. in your character class [], and you can avoid having to escape the dash - by placing it at the beginning or end of your character class.

您可以使用 \p {L} 可以匹配任何语言的任何字母。参见下面的示例:

You can use \p{L} which matches any kind of letter from any language. See the example below:

string[] names = { "Brendán", "Jóhn", "Jason" };
Regex rgx      = new Regex(@"^\p{L}+$");
foreach (string name in names)
    Console.WriteLine("{0} {1} a valid name.", name, rgx.IsMatch(name) ? "is" : "is not");

// Brendán is a valid name.
// Jóhn is a valid name.
// Jason is a valid name.

或者只是将所需字符添加到字符类 []

Or simply just add the desired characters to your character class [] you want to include.

@"^[a-zA-Z0-9áéíóú@#%&',.\s-]+$"

这篇关于正则表达式中的Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆