C#中的正则表达式中的外语字符 [英] Foreign language characters in Regular expression in C#

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

问题描述

在C#代码中,我试图传递中文字符:" 中文ABC123".

In C# code, I am trying to pass chinese characters: " 中文ABC123".

当我通常使用"^[a-zA-Z0-9\s]+$"使用字母数字时,

When I use alphanumeric in general using "^[a-zA-Z0-9\s]+$",

它不能通过"中文ABC123",并且正则表达式验证失败.

it doesn't pass for "中文ABC123" and regex validation fails.

我还需要为C#添加哪些其他表达式?

What other expressions do I need to add for C#?

推荐答案

要匹配来自任何语言的任何字母字符,请使用:

To match any letter character from any language use:

\p{L}

如果您还想匹配数字:

[\p{L}\p{Nd}]+

\p{L} ...匹配unicode类别字母的字符.
                    p {Lt} \ p {Lm} \ p {Lo}]
                  \p{Ll} ...较低的字母(abc)
                  \p{Lu} ...大写字母匹配(ABC)
                  \p{Lt}字母匹配
                  \p{Lm} ...匹配                    \p{Lo}字母不匹配的情况下.... (中文)

\p{L} ... matches a character of the unicode category letter.
                it is the short form for [\p{Ll}\p{Lu}\p{Lt}\p{Lm}\p{Lo}]
                  \p{Ll} ... matches lowercase letters. (abc)
                  \p{Lu} ... matches uppercase letters. (ABC)
                  \p{Lt} ... matches titlecase letters.
                  \p{Lm} ... matches modifier letters.
                  \p{Lo} ... matches letters without case. (中文)

\p{Nd} ...匹配unicode类别十进制数字的字符.

\p{Nd} ... matches a character of the unicode category decimal digit.

只需将^[a-zA-Z0-9\s]+$替换为^[\p{L}0-9\s]+$

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

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