How does one find the currency value in a string?

我的未来我决定 提交于 2020-01-10 03:14:38

问题


I'm writing a small tool to extract a bunch of values from a string (usually a tweet).

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)

where x.text is for example "Taxi London £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.


回答1:


>>> re.search(ur'([£$€])(\d+(?:\.\d{2})?)', s).groups()
(u'\xa3', u'6.50')
  • [£$€] 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.




回答2:


I've altered Marcog's regex altered a bit


    re.search(ur'([£\$€])(\d+(?:\.\d{2})?)', s).groups()

by escaping the dollar sign.



来源:https://stackoverflow.com/questions/4862827/how-does-one-find-the-currency-value-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!