验证Unicode Nam

2024-04-20 12:14:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在ASCII中,验证一个名称并不困难:只需确保所有字符都是按字母顺序排列的。在

但是在Unicode(utf-8)中呢?如何确保给定字符串中没有逗号或下划线(ASCII范围之外)?在

(最好是Python)


Tags: 字符串名称字母asciiunicode字符utf逗号
3条回答

也许unicodedata module对这个任务很有用。尤其是category()函数。对于现有的unicode类别,请查看unicode.org。然后你可以过滤标点字符等

根据您如何定义“name”,您可以根据以下正则表达式检查它:

^\w+$

但是,这将允许数字和下划线。要排除它们,您可以针对以下各项进行第二次测试:

^{pr2}$

让你的支票在比赛中失败。这两种方法可以结合如下:

^(?:(?![\d_])\w)+$

但是出于regex性能的原因,我宁愿做两个单独的检查。在

来自the docs

\w

When the LOCALE and UNICODE flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus whatever characters are defined as alphanumeric for the current locale. If UNICODE is set, this will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database.

只需将bytestring(您的utf-8)转换为unicode对象,然后检查是否所有字符都是字母:

s.isalpha()

此方法依赖于bytestrings的区域设置。在

相关问题 更多 >