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

发布于 2021-01-29 15:19:57

在Java中,我可以使用regex:\p{Sc}检测文本中的货币符号。Python中的等效项是什么?

关注者
0
被浏览
174
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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

    >>> 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]
    ¢
    

    更新

    交替使用方式unicodedata.category

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看