什么是货币符号的正则表达式?

Lan*_*anc 28 python regex

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

fal*_*tru 42

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

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

UPDATE

使用的替代方式unicodedata.category:

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

  • 谢谢!我不知道'正则表达式'包与're'包不同. (4认同)
  • `regex` is*awesome*. [That's just the tip of the iceberg.](https://pypi.python.org/pypi/regex) (2认同)

Phi*_*lip 27

如果你想坚持使用re,请手动提供Sc中字符:

u"[$¢£¤¥?????????\u20a0-\u20bd\ua838\ufdfc\ufe69\uff04\uffe0\uffe1\uffe5\uffe6]"
Run Code Online (Sandbox Code Playgroud)

会做.

  • 您可以自动生成一组货币字符:`currency_symbols = u''.join(unichr(i)for i in range(0xffff)if unicodedata.category(unichr(i))=='Sc')` (6认同)
  • 这包含在`\ u20a0-\u20bd`中:€是`\ u20ac`. (3认同)