Monday, December 10, 2012

Python: re (regexp): index of matches: difference between findall and finditer

  • using the findall method of a regexp will return all matches to the regular experession as a list
  • using finditer will return an iterator which you can use to interate through the matches
  • if m is the result of iterator.next() then m.span() will give you the start and end indexes of the match and m.group() gives you the match
  • If you have groups in the regexp e.g r"a(.*?)b" then m.group() will return the WHOLE matched expression and m.span() will return the corresponding start and stop indexes. To get just the particular group, for the expression given above, you have to do m.span(1) and m.group(1)

No comments:

Post a Comment