正则表达式以欧元英镑和美元的价格 [英] Regex for prices with euros pounds and dollars

查看:39
本文介绍了正则表达式以欧元英镑和美元的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 正则表达式查找字符串中的所有价格.到目前为止,我只是无法正确管理符号.此代码,输入:'happy$37.54000happy$34$3454$3333€27.80€3.00.33.2£27.000'

I am using python regex to find all prices in a string. Thus far I am only having trouble managing the symbols correctly. This code, with the input: 'happy$37.54000happy$34$3454$3333€27.80€3.00.33.2£27.000'

   import sys
   import re
   price = sys.argv[1]
   new = re.findall(r'[\$\20AC\00A3]{1}\d+\.?\d{0,2}',price,re.UNICODE)
   for prices in new:
       print prices

输出:

$37.54
$34
$3454    
$3333

我想要的是:

$37.54
$34
$3454
$3333
€27.80
€3.00    
£27.00

如果我将欧元符号添加到代码中,则该文件无法编译,因为它不是 unicode 字符.我在想,因为 20AC 是欧元符号的 unicode,而 \00A3 是英镑符号的 unicode,它可以工作,但它没有.

If I add the euro sign into the code the file cannot compile given that it is not a unicode character. I was thinking that since 20AC is the unicode for the euro symbol and \00A3 is the unicode for the pound symbol that that would work, but it does not.

我认为问题在于代码的这一部分:...

I believe that the issues lies in this part of the code:...

[\$\20AC\00A3]...

任何帮助将不胜感激

为未来的人编辑 - 这是最好的代码答案:

EDIT FOR FUTURE PEOPLE - THIS IS THE BEST CODE ANSWER:

# -*- coding: utf-8 -*-
import sys
import re
price = sys.argv[1]
new = re.findall(r'[$€£]{1}\d+\.?\d{0,2}',price,re.UNICODE)
for prices in new:
    print prices

推荐答案

您需要在正则表达式中为您的 unicode 字符代码添加 \u.即

You need to add \u for your unicode character codes in your regex. i.e

new = re.findall(ur'[\$\u20AC\u00A3]{1}\d+\.?\d{0,2}',string,re.UNICODE)

https://docs.python.org/2/tutorial/介绍.html#unicode-strings

这篇关于正则表达式以欧元英镑和美元的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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