什么是货币符号的正则表达式? [英] What is regex for currency symbol?

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

问题描述

在 Java 中,我可以使用正则表达式:\p{Sc}检测文本中的货币符号.Python 中的等价物是什么?

解决方案

如果您使用 regex,则可以使用 unicode 类别 包:

<预><代码>>>>导入正则表达式>>>regex.findall(r'\p{Sc}', '$99.99/€77') # Python 3.x['$', '€']

<小时><预><代码>>>>regex.findall(ur'\p{Sc}', u'$99.99/€77') # Python 2.x (NoteL Unicode 文字)[u'$', u'\xa2']>>>打印_[1]¢

更新

使用unicodedata.category的替代方法:

<预><代码>>>>导入 unicodedata>>>[ch for ch in '$99.99/€77' if unicodedata.category(ch) == 'Sc']['$', '€']

In java I can use the regex : \p{Sc} for detecting currency symbol in text. What is the equivalent in Python?

解决方案

You can use the unicode category if you use regex package:

>>> import regex
>>> regex.findall(r'\p{Sc}', '$99.99 / €77')  # Python 3.x
['$', '€']


>>> regex.findall(ur'\p{Sc}', u'$99.99 / €77')  # Python 2.x (NoteL unicode literal)
[u'$', u'\xa2']
>>> print _[1]
¢

UPDATE

Alterantive way using unicodedata.category:

>>> import unicodedata
>>> [ch for ch in '$99.99 / €77' if unicodedata.category(ch) == 'Sc']
['$', '€']

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

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