如何找到字符串中的货币值? [英] How does one find the currency value in a string?
问题描述
我正在写一个小工具,用于从字符串(通常是一条推文)中提取一堆值。
I'm writing a small tool to extract a bunch of values from a string (usually a tweet).
字符串可以由单词和数字以及以货币符号(£,$,€等)为前缀的金额和一些井号(#foo #bar)。我在appEngine上运行,并使用tweepy引入了推文。
The string could consist of words and numbers along with an amount prefixed by a currency symbol (£,$,€ etc.) and a number of hashtags (#foo #bar). I'm running on appEngine and using tweepy to bring in the tweets.
我现在要查找值的当前代码如下:
The current code I have to find the values is below:
tagex = re.compile(r'#.*')
curex = re.compile(ur'[£].*')
for x in api.user_timeline(since_id = t.lastimport):
tags = re.findall(tagex, x.text)
amount = re.findall(curex, x.text)[0]
logging.info("Text: " + x.text)
logging.info("Tags: " + str(tags))
logging.info("Amount: " + amount)
其中x.text例如 Taxi London£6.50 #projectfoo #clientmeeting
where x.text is for example "Taxi London £6.50 #projectfoo #clientmeeting"
tagex发现主题标签很好,但我无法使curx提取当前获得的金额:
金额:£6.50 #projectfoo #clientmeeting。
The tagex finds the hashtags fine, but I can't get curex to extract the amount currently I get: Amount: £6.50 #projectfoo #clientmeeting.
我还需要分离货币符号,以便将其作为浮动金额,但这以后应该很简单。
I also need to separate off the currency symbol so as to get the amount as a float, but that should be pretty simple later.
推荐答案
>>> re.search(ur'([£$€])(\d+(?:\.\d{2})?)', s).groups()
(u'\xa3', u'6.50')
-
[ £$€]
匹配一个货币符号 -
\d +(?: \.\d {2})
匹配一个或多个数字,后跟一个可选的小数点,然后是恰好两个数字 -
()
分别捕获符号和金额 [£$€]
matches one currency symbol\d+(?:\.\d{2})
matches one or more digits followed by an optional decimal point followed by exactly two digits- The
()
's capture the symbol and amount separately
正则表达式的问题是。*
会匹配所有内容并且是贪婪的,因此在正则表达式的末尾它会匹配随后的所有内容。
The problem with your regex is that .*
matches anything and is greedy, so at the end of a regex it matches everything that follows.
这篇关于如何找到字符串中的货币值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!