Python正则表达式大写unicode字 [英] Python regex uppercase unicode word

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

问题描述

我需要查找多种语言的缩写文本.当前 regex 是:

import regex as repattern = re.compile('(?:[\w]\.)+', re.UNICODE | re.MULTILINE | re.DOTALL | re.VERSION1)pattern.findall("美国美国")

结果中不需要 u.s.a,我只需要大写文本.[A-Z] 不适用于除英语以外的任何语言.

解决方案

您需要使用 Unicode 字符属性来匹配它们.re 不支持字符属性,但regex 确实如此.

<预><代码>>>>regex.findall(ur'\p{Lu}', u'ÜìÑ')[u'\xdc', u'\xd1']

I need to find abbreviations text in many languages. Current regex is:

import regex as re
pattern = re.compile('(?:[\w]\.)+', re.UNICODE | re.MULTILINE | re.DOTALL | re.VERSION1)
pattern.findall("U.S.A. u.s.a.")

I don't need u.s.a in the result, i need only uppercase text. [A-Z] won't work in any language except english.

解决方案

You need to use a Unicode character property in order to match them. re does not support character properties, but regex does.

>>> regex.findall(ur'\p{Lu}', u'ÜìÑ')
[u'\xdc', u'\xd1']

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

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