Larsson
Larsson

Reputation: 39

Use Re.search to extract wanted text

My Script currently prints

<span class="price">€179.95</span>

I am trying to use re.search to extract just the price in €, so in this example I want to print "179", but unfortunately I am struggling with the use of re.search and advice or links to tutorials would be helpful.

Thanks,

Upvotes: 0

Views: 58

Answers (1)

Neil
Neil

Reputation: 14313

I'd use the following regex:

€(\d+)

Here's a regex 101 to play with it on:

https://regex101.com/r/8WYzaK/2

Additionally, you should be using findall for this:

import re
span = '<span class="price">€179.95</span>'
print(re.findall('€(\d+)',span))

If encoding doesn't work:

import re
span = '<span class="price">€179.95</span>'
print(re.findall('\u20AC(\d+)',span))

Upvotes: 3

Related Questions