0

I'm trying to receive the prices from the following string:

"€29,95 ipv €69,95 – ezCast M2 Original "

With the following regex:

[\$\£\€](\d+(?:.\d{1,2})?)

According to regex testers the above regex works fine (I receive 29,95 and 69,95 without currency, so that's good)... But in python it doesn't. I think it's because of the fact python unicodes strings. Because if I print the string on my screen I get:

[u'\u20ac29,95 ipv \u20ac69,95 \u2013 ezCast M2 Original']

I tried the following codes:

p = re.findall('[\$\£\€](\d+(?:.\d{1,2})?)',str(prices))
p = re.findall(u'[\$\£\€](\d+(?:.\d{1,2})?)',str(prices))
p = re.findall(ur'[\$\£\€](\d+(?:.\d{1,2})?)',str(prices))
p = re.findall(r'[\$\£\€](\d+(?:.\d{1,2})?)',str(prices))

None of those work... But the one below DOES work:

p = re.compile('(\d+(?:.\d{1,2})?)')
        #
        for m in p.findall(str(prices)):
            print m

But then I receive ALL numbers and I just want the numbers behind a currency.

Anyone who can help me out?

1
  • 1
    Try r'[$£€](\d+(?:\.\d{1,2})?)' Commented Jul 4, 2014 at 12:54

3 Answers 3

2

Convert the string into unicode by decoding it.

>>> prices = "€29,95 ipv €69,95  ezCast M2 Original "
>>> re.findall(ur'[\$\\€](\d+(?:.\d{1,2})?)', prices.decode('utf-8'))
[u'29,95', u'69,95']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It brought me on the right path! I only needed to add .pop() to prices to get the first item from the list. .decode('utf-8') wasn't necessary anymore :)
Useful and concise answer. :) +1
0

You can use this regex :

.*?(\d+,\d+).*?ipv.*?(\d+,\d+).*?

It ignores € sign and depends on "ipv" separator. You must get \1 and \2 elements

Comments

0

You need to encode the string:

>>> prices = u'\u20ac29,95 ipv \u20ac69,95 \u2013 ezCast M2 Original'
>>> p = re.findall('[\$\£\€](\d+(?:.\d{1,2})?)',prices.encode('utf8'))
>>> p
['29,95', '69,95']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.