Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python re, multiple matching groups

I have a string:

s = '&nbsp;<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'&nbsp;.*(>\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!

like image 996
tommy.carstensen Avatar asked Aug 30 '26 22:08

tommy.carstensen


2 Answers

>>> 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']
>>> 
like image 194
zhangyangyu Avatar answered Sep 01 '26 12:09

zhangyangyu


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.