How to get multiple regex matches in python? -
i have text:
<div class="additional-details"> <div class="mark-container"> <input type="checkbox" id="comp-80174649" value="80174649" data-heading-code="2550"/> <label for="comp-80174649">???</label> <a href="#" class="compare-link" id="compare-link-1" data-compare="/80174649/2550/" data-drop-down-id="compare-content-1" data-drop-down-content-id="compare-content" data-drop-down-class="drop-down-compare" etc... data-compare="/8131239/2550/" i trying scrape inside data-compare="here" (i have multiple matches).
i know how in c#, using matchcollection, in python pretty confused re.search, re.match , i've noticed regex working in c# not working in python.
could explain how done ?
re.findall can used find matches in list.
>>> import re >>> s = '<div cla' # whole string here >>> result = re.findall('data-compare="([\d/]+)"', s) >>> print result ['/80174649/2550/', '/8131239/2550/'] explanation
the desired output '/80174649/2550/' has numbers , forward slash, we'll targeting that.
in ([\d/]+), [\d/] means match either number (signified \d) or forward slash /.
then + symbol means preceding pattern [\d/] can occur multiple times since have multiple numbers , /.
the enclosing parentheses means enclosed pattern [\d/]+ should captured , returned.
Comments
Post a Comment