Wednesday, 15 February 2012

python - Find all matches between two strings with regex -



python - Find all matches between two strings with regex -

i starting utilize regex first time , trying utilize parse info html table. trying grab between <tr > , </tr> tags, , create similar regex 1 time again create json array.

i tried using matching first grouping , not of rest.

<tr >(.*?)</tr>

how create find matches between tags?

although using regex job bad thought (there many ways things go wrong), pattern correct.

returning matches python

the question becomes returning matches or capture groups in python. there 2 basic ways:

finditer findall

with finditer

for match in regex.finditer(subject): print("the overall match: ", match.group(0)) print("group 1: ", match.group(1))

with findall

findall bit strange. when have capture groups, access both capture groups , overall match, have wrap original regex in parentheses (so overall match captured too). in case, if wanted able access both outside of tags , within (which captured grouping 1), regex become: (<tr >(.*?)</tr>). do:

matches = regex.findall(subject) if len(matches)>0: match in matches: print ("the overall match: ",match[0]) print ("group 1: ",match[1])

python regex

No comments:

Post a Comment