How does one find the currency value in a string?

廉价感情. 提交于 2019-11-30 23:41:00
>>> 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.

I've altered Marcog's regex altered a bit


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

by escaping the dollar sign.

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