# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"<th(?:| | .*?)>\s*<a(?:| | .*?)>\s*([\d.]+)\s*</a>\s*</th>\s*<td(?:| | .*?)>\s*stable\s*</td>"
test_str = (" <table cellpadding=\"2\" cellspacing=\"1\" style=\"width: 100%; border: 0px;\">\n"
" <tr style=\"background-color: #CCCCCC;\">\n"
" <th colspan=\"5\">Available Releases</th>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#ffffff\">Version</th>\n"
" <th valign=\"top\" bgcolor=\"#ffffff\">State</th>\n"
" <th valign=\"top\" bgcolor=\"#ffffff\">Release Date</th>\n"
" <th valign=\"top\" bgcolor=\"#ffffff\">Downloads</th>\n"
" <th valign=\"top\" bgcolor=\"#ffffff\"> </th>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/5.1.2\">5.1.2</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">stable</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-12-07</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-5.1.2.tgz\">apcu-5.1.2.tgz</a> (105.7kB)</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=5.1.2\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.10\">4.0.10</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">stable</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-12-07</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.10.tgz\">apcu-4.0.10.tgz</a> (116.5kB) <a href=\"/package/APCu/4.0.10/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.10\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/5.1.0\">5.1.0</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-11-21</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-5.1.0.tgz\">apcu-5.1.0.tgz</a> (106.8kB)</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=5.1.0\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.8\">4.0.8</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">stable</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2015-11-20</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.8.tgz\">apcu-4.0.8.tgz</a> (116.3kB) <a href=\"/package/APCu/4.0.8/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.8\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.7\">4.0.7</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-10-11</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.7.tgz\">apcu-4.0.7.tgz</a> (115.9kB) <a href=\"/package/APCu/4.0.7/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.7\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.6\">4.0.6</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-06-12</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.6.tgz\">apcu-4.0.6.tgz</a> (116.9kB) <a href=\"/package/APCu/4.0.6/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.6\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.5\">4.0.5</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-06-11</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.5.tgz\">apcu-4.0.5.tgz</a> (116.9kB) <a href=\"/package/APCu/4.0.5/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.5\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.4\">4.0.4</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-03-01</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.4.tgz\">apcu-4.0.4.tgz</a> (116.9kB) <a href=\"/package/APCu/4.0.4/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.4\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.3\">4.0.3</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2014-01-27</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.3.tgz\">apcu-4.0.3.tgz</a> (116.9kB) <a href=\"/package/APCu/4.0.3/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.3\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.2\">4.0.2</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2013-09-14</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.2.tgz\">apcu-4.0.2.tgz</a> (117.5kB) <a href=\"/package/APCu/4.0.2/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.2\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.1\">4.0.1</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2013-04-30</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.1.tgz\">apcu-4.0.1.tgz</a> (103.4kB) <a href=\"/package/APCu/4.0.1/windows\"><img src=\"/gifs/windows-icon.png\" />DLL</a></td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.1\">Changelog</a>]</small></td>\n"
" </tr>\n"
" <tr>\n"
" <th valign=\"top\" bgcolor=\"#cccccc\"><a href=\"/package/APCu/4.0.0\">4.0.0</a></th>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">beta</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\">2013-03-26</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><a href=\"/get/apcu-4.0.0.tgz\">apcu-4.0.0.tgz</a> (101.8kB)</td>\n"
" <td valign=\"top\" bgcolor=\"#e8e8e8\"><small>[<a href=\"/package-changelog.php?package=APCu&release=4.0.0\">Changelog</a>]</small></td>\n"
" </tr>\n"
" </table>")
matches = re.search(regex, test_str)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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