# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"<tr class='(.)'><td class='TR(.)'><a href='http://www\.openstreetmap\.org\/\?mlon=([-\d\.]+)&mlat=([-\d\.]+)&zoom=\d+' target='_blank'><img src='img\/flags\/(\w+)\.gif' class='flag' width='\d+px' title='(.+)' alt='\w+' border='0'><\/a> <a href='router_detail\.php\?FP=([a-zA-Z0-9]+)' target='_blank'>([^\<]+)<\/a><\/td><td class='TDb'><table cellspacing='0' cellpadding='0' class='bwb'><tr title='([^']+)'><td class='bwr.?'><img src='img/bar/\d+.png' width='\d+px' height='15px' alt='\d+'></td><td> <small> \d+</small></td></tr></table></td><td class='TDc.?'>([^<]+)</td><td class='TDS'><table class='iT'><tr><td class='iT'>([^\[]+)\[<a class='who' href='/cgi\-bin/whois.pl\?ip=[\d\.]+' target='_blank'>([\d\.]+)</a>\]</td>(<td><img src='img/status/Fast.png' title='Fast Server' alt='Fast Server'></td>)?(<td><img src='img/status/Exit.png' title='Exit Server' alt='Exit Server'></td>)?(<td><img src='img/status/Dir.png' title='Directory Server' alt='Directory Server'></td>)?(<td><img src='img/status/Guard.png' title='Guard Server' alt='Guard Server'></td>)?(<td><img src='img/status/Stable.png' title='Stable Server' alt='Stable Server'></td>)?(<td><img src='img/status/Authority.png' title='Authority Server' alt='Authority Server'/></td>)?<td><img src='[^']+' title='([^']+)' alt='[^']+'></td>(<td><img src='[^']+' title='([^']*)' alt='[^']*'></td>)?</tr></table></td><td class='TDc'>(<b>)?(\d*)(</b>)?</td><td class='TDc'>(<b>)?([None\d]*)(</b>)?</td><td class='(F\d)'></td></tr>"
test_str = ("<tr class='r'><td class='TRR'><a href='http://www.openstreetmap.org/?mlon=-0.1300&mlat=51.5000&zoom=6' target='_blank'><img src='img/flags/gb.gif' class='flag' width='18px' title='//GB' alt='GB' border='0'></a> <a href='router_detail.php?FP=dc358f29426fb682f96f37fb4f3538edbb94b3d4' target='_blank'>czu5kXA8zLE0</a></td><td class='TDb'><table cellspacing='0' cellpadding='0' class='bwb'><tr title='817 KBs'><td class='bwr'><img src='img/bar/1.png' width='69px' height='15px' alt='817'></td><td> <small> 817</small></td></tr></table></td><td class='TDcb'>3 d</td><td class='TDS'><table class='iT'><tr><td class='iT'>212.38.166.231 [<a class='who' href='/cgi-bin/whois.pl?ip=212.38.166.231' target='_blank'>212.38.166.231</a>]</td><td><img src='img/status/Fast.png' title='Fast Server' alt='Fast Server'></td><td><img src='img/status/Exit.png' title='Exit Server' alt='Exit Server'></td><td><img src='img/status/Dir.png' title='Directory Server' alt='Directory Server'></td><td><img src='img/status/Guard.png' title='Guard Server' alt='Guard Server'></td><td><img src='img/status/Stable.png' title='Stable Server' alt='Stable Server'></td><td><img src='img/os-icons/Linux.png' title='Tor 0.2.4.22 on Linux' alt='Tor 0.2.4.22 on Linux'></td></tr></table></td><td class='TDc'>9001</td><td class='TDc'>9030</td><td class='F0'></td></tr>\n"
"<tr class='r'><td class='TRr'><a href='http://www.openstreetmap.org/?mlon=-93.3084&mlat=37.2999&zoom=6' target='_blank'><img src='img/flags/us.gif' class='flag' width='18px' title='Springfield/Missouri/US' alt='US' border='0'></a> <a href='router_detail.php?FP=643b8a73713e2e948c5e3359d5bf5e4c8249aee2' target='_blank'>d00dstor</a></td><td class='TDb'><table cellspacing='0' cellpadding='0' class='bwb'><tr title='2 KBs'><td class='bwr'><img src='img/bar/1.png' width='1px' height='15px' alt='2'></td><td> <small> 2</small></td></tr></table></td><td class='TDc'>13 h</td><td class='TDS'><table class='iT'><tr><td class='iT'>173-18-211-201.client.mchsi.com [<a class='who' href='/cgi-bin/whois.pl?ip=173.18.211.201' target='_blank'>173.18.211.201</a>]</td><td><img src='img/status/Dir.png' title='Directory Server' alt='Directory Server'></td><td><img src='img/os-icons/Linux.png' title='Tor 0.2.4.22 on Linux' alt='Tor 0.2.4.22 on Linux'></td></tr></table></td><td class='TDc'>9001</td><td class='TDc'>9030</td><td class='F0'></td></tr>\n"
"<tr class='r'><td class='TRr'><a href='http://www.openstreetmap.org/?mlon=15.5000&mlat=49.7500&zoom=6' target='_blank'><img src='img/flags/cz.gif' class='flag' width='18px' title='//CZ' alt='CZ' border='0'></a> <a href='router_detail.php?FP=fb2b4193f34aaa980d0e8c6ec2aa71417525f4b5' target='_blank'>d1n</a></td><td class='TDb'><table cellspacing='0' cellpadding='0' class='bwb'><tr title='1780 KBs'><td class='bwr1'><img src='img/bar/2.png' width='66px' height='15px' alt='1780'></td><td> <small> 1780</small></td></tr></table></td><td class='TDcb'>2 d</td><td class='TDS'><table class='iT'><tr><td class='iT'>185.8.237.27 [<a class='who' href='/cgi-bin/whois.pl?ip=185.8.237.27' target='_blank'>185.8.237.27</a>]</td><td><img src='img/status/Fast.png' title='Fast Server' alt='Fast Server'></td><td><img src='img/status/Dir.png' title='Directory Server' alt='Directory Server'></td><td><img src='img/status/Guard.png' title='Guard Server' alt='Guard Server'></td><td><img src='img/status/Stable.png' title='Stable Server' alt='Stable Server'></td><td><img src='img/os-icons/Linux.png' title='Tor 0.2.4.22 on Linux' alt='Tor 0.2.4.22 on Linux'></td></tr></table></td><td class='TDc'>9001</td><td class='TDc'>9030</td><td class='F0'></td></tr>\n"
"<tr class='r'><td class='TRR'><a href='http://www.openstreetmap.org/?mlon=4.9167&mlat=52.3500&zoom=6' target='_blank'><img src='img/flags/nl.gif' class='flag' width='18px' title='Amsterdam/Noord-Holland/NL' alt='NL' border='0'></a> <a href='router_detail.php?FP=6ef897645b79b6cb35e853b32506375014de3621' target='_blank'>d6relay</a></td><td class='TDb'><table cellspacing='0' cellpadding='0' class='bwb'><tr title='383 KBs'><td class='bwr'><img src='img/bar/1.png' width='32px' height='15px' alt='383'></td><td> <small> 383</small></td></tr></table></td><td class='TDcb'>22 d</td><td class='TDS'><table class='iT'><tr><td class='iT'>charles.d6.nl [<a class='who' href='/cgi-bin/whois.pl?ip=82.161.239.177' target='_blank'>82.161.239.177</a>]</td><td><img src='img/status/Fast.png' title='Fast Server' alt='Fast Server'></td><td><img src='img/status/Dir.png' title='Directory Server' alt='Directory Server'></td><td><img src='img/status/Guard.png' title='Guard Server' alt='Guard Server'></td><td><img src='img/status/Stable.png' title='Stable Server' alt='Stable Server'></td><td><img src='img/os-icons/Linux.png' title='Tor 0.2.4.22 on Linux' alt='Tor 0.2.4.22 on Linux'></td></tr></table></td><td class='TDc'>110</td><td class='TDc'><b>80</b></td><td class='F0'></td></tr>\n")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html