Saqib Saqib - 1 month ago 20
Python Question

Python Regular Expression example

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 first bold text after string "Your number is"?

Answer
import re
m = re.search("Your number is <b>(\d+)</b>",
      "xxx Your number is <b>123</b>  fdjsk")
if m:
    print m.groups()[0]
Comments