Alex Rivera | Logout

How to use regex to parse a number from HTML?

Asked 2012-06-23T16:13:02.567
52

I want to write a simple regular expression in Python that extracts a number from HTML. The HTML sample is as follows:

Your number is <b>123</b>

Now, how can I extract "123", i.e. the contents of the first bold text after the string "Your number is"?

Edit
Report

1 Answer

12
import re
x = 'Your number is <b>123</b>'
re.search('(?<=Your number is )<b>(\d+)</b>',x).group(0)

this searches for the number that follows the 'Your number is' string

answered 2012-06-23T16:20:55.943

Your Answer