I have a string:
s = ' <span>Mil<\/span><\/th><td align=\"right\" headers=\"Y0 i7\">112<\/td><td align=\"right\" headers=\"Y1 i7\">113<\/td><td align=\"right\" headers=\"Y2 i7\">110<\/td><td align=\"right\" headers=\"Y3 i7\">107<\/td><td align=\"right\" headers=\"Y4 i7\">105<\/td><td align=\"right\" headers=\"Y5 i7\">95<\/td><td align=\"right\" headers=\"Y6 i7\">95<\/td><td align=\"right\" headers=\"Y7 i7\">87<\/td><td align=\"right\" headers=\"Y8 i7\">77<\/td><td align=\"right\" headers=\"Y9 i7\">74<\/td><td align=\"right\" headers=\"Y10 i7\">74<\/td><\/tr>'
I want to extract these numbers from the string:
112 113 110 107 105 95 95 87 77 74 74
I am no expert on regular expressions, so can anyone tell me, why this isn't returning any matches:
p = re.compile(r' .*(>\d*<\\/td>.*)*<\\/tr>')
m = p.match(s)
I'm sure there is an html/xml parsing module that can solve my problem and I could also just split the string and work on that output, but I really want to do it with the re module. Thanks!
>>> r = re.compile(r'headers="Y\d+ i\d+">(\d+)<\\/td>')
>>> r.findall(s)
['112', '113', '110', '107', '105', '95', '95', '87', '77', '74', '74']
>>>
All of the numbers you want are in between ">" and "<". So, you can just do this:
re.findall(">(\d+)<", s)
output:
['112', '113', '110', '107', '105', '95', '95', '87', '77', '74', '74']
Basically, it's saying get every stream of digits that is between ">" and "<". Then, with set, you can get only the unique ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With