【问题标题】:What is regex for currency symbol?货币符号的正则表达式是什么?
【发布时间】:2014-11-16 16:42:20
【问题描述】:

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

【问题讨论】:

    标签: python regex


    【解决方案1】:

    使用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']
    ['$', '€']
    

    【讨论】:

    【解决方案2】:

    如果您想坚持使用re,请提供characters from Sc manually

    u"[$¢£¤¥֏؋৲৳৻૱௹฿៛\u20a0-\u20bd\ua838\ufdfc\ufe69\uff04\uffe0\uffe1\uffe5\uffe6]"
    

    会的。

    【讨论】:

    • 以下哪个是欧元符号?
    • 包含在\u20a0-\u20bd 中:€ 是\u20ac
    • 您可以自动生成货币字符集:currency_symbols = u''.join(unichr(i) for i in range(0xffff) if unicodedata.category(unichr(i)) == 'Sc')
    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2010-10-23
    • 1970-01-01
    相关资源
    最近更新 更多