# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"<div[\s\S]*?id=\"variationsTwister[\s\S]*?<span[\s\S]*?a-dropdown-container[\s\S]*?<select[\s\S]*?<\/select>[\s\S]*?Color:<\/span>[\s\S]*?<span[\s\S]*?>([\s\S]*?)<\/span>"
test_str = ("<div id=\"variationsTwister\" class=\"a-section a-spacing-mini\">\n"
" \n\n"
" \n\n\n\n\n\n\n\n\n"
" \n"
" \n\n"
" <div class=\"a-section a-spacing-micro\">\n"
" <span class=\"a-size-base\">Size:</span>\n"
" <span class=\"a-size-base a-text-bold\"></span>\n"
" </div>\n"
" <span class=\"a-dropdown-container\"><select name=\"\" autocomplete=\"off\" data-a-native-class=\"olpTwisterDropdown\" tabindex=\"-1\" class=\"a-native-dropdown a-spacing-small olpTwisterDropdown\">\n"
" <option value=\"/gp/offer-listing/B00NNA39IE/ref=olp_twister_all?ie=UTF8&mv_color_name=3&mv_size_name=all\">\n"
" Lowest offer for each\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3QSKO/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=0\">\n"
" 5 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3QU1G/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=1\">\n"
" 5.5 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3QX72/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=2\">\n"
" 6 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3QYQC/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=3\">\n"
" 6.5 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3R0PQ/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=4\">\n"
" 7 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3R23Q/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=5\">\n"
" 7.5 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3R3M6/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=6\" selected=\"\">\n"
" 8 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3R6LO/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=7\">\n"
" 8.5 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3R9IY/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=8\">\n"
" 9 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3RBE6/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=9\">\n"
" 9.5 M US\n"
" </option>\n"
" <option value=\"/gp/offer-listing/B00MC3RCPY/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=10\">\n"
" 10 M US\n"
" </option>\n"
" </select><span tabindex=\"-1\" id=\"olpTwisterDropdownAUI\" class=\"a-button a-button-dropdown a-button-small a-spacing-small\" style=\"min-width: 0.583333%;\"><span class=\"a-button-inner\"><span class=\"a-button-text a-declarative\" data-action=\"a-dropdown-button\" role=\"button\" tabindex=\"0\" aria-hidden=\"true\"><span class=\"a-dropdown-prompt\">\n"
" 8 M US\n"
" </span></span><i class=\"a-icon a-icon-dropdown\"></i></span></span></span>\n"
" <div class=\"a-section a-spacing-micro\">\n"
" <span class=\"a-size-base\">Color:</span>\n"
" <span class=\"a-size-base a-text-bold\">Taupe</span>\n"
" </div>\n"
" <ul class=\"a-unordered-list a-nostyle a-button-list a-declarative a-button-toggle-group a-horizontal a-spacing-small\" role=\"radiogroup\" data-action=\"a-button-group\">\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-0\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B0002MB0DM/ref=olp_twister_child?ie=UTF8&mv_color_name=0&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-0-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41jl1EwZjHL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-1\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B005BFV1DO/ref=olp_twister_child?ie=UTF8&mv_color_name=1&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-1-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41XqEgaqwtL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-2\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00MC3SFXW/ref=olp_twister_child?ie=UTF8&mv_color_name=2&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-2-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41cNhnckTFL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-selected a-button-thumbnail a-button-toggle\" id=\"a-autoid-3\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00MC3R3M6/ref=olp_twister_child?ie=UTF8&mv_color_name=3&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-3-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41p2f8xA8TL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-4\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B011SBR9PS/ref=olp_twister_child?ie=UTF8&mv_color_name=4&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-4-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/311Xw94d26L._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-5\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B07FYS34S4/ref=olp_twister_child?ie=UTF8&mv_color_name=5&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-5-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41r4qvtmEkL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-6\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00MC3TVSK/ref=olp_twister_child?ie=UTF8&mv_color_name=6&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-6-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/31YEDbIcNwL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-7\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B016W1Z1RC/ref=olp_twister_child?ie=UTF8&mv_color_name=7&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-7-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41plM8TSFBL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-thumbnail a-button-toggle\" id=\"a-autoid-8\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B0007L7WPS/ref=olp_twister_child?ie=UTF8&mv_color_name=8&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-8-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41NGM8gp7hL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </a></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-9\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-9-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-9-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41WBKxWvoLL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </span></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-10\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-10-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-10-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41ap6ahxMRL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </span></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-11\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-11-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-11-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/41QD5K54hPL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </span></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-disabled a-button-thumbnail a-button-toggle\" id=\"a-autoid-12\"><span class=\"a-button-inner\"><input disabled=\"disabled\" class=\"a-button-input\" type=\"submit\" aria-labelledby=\"a-autoid-12-announce\"><span class=\"a-button-text\" aria-hidden=\"true\" id=\"a-autoid-12-announce\">\n"
" <img alt=\"\" src=\"https://images-na.ssl-images-amazon.com/images/I/31qtQ78OWiL._SX35_SY35_CR,0,0,35,35_.jpg\" height=\"35\" width=\"35\">\n"
" </span></span></span>\n"
" </span></li>\n"
" <li class=\"a-spacing-mini\"><span class=\"a-list-item\">\n"
" <span class=\"a-button a-button-normal a-button-toggle\" id=\"a-autoid-13\"><span class=\"a-button-inner\"><a href=\"/gp/offer-listing/B00NNA39IE/ref=olp_twister_all?ie=UTF8&mv_color_name=all&mv_size_name=6\" class=\"a-button-text\" role=\"button\" id=\"a-autoid-13-announce\">\n"
" Lowest offer for each\n"
" </a></span></span>\n"
" </span></li>\n"
" </ul>\n\n"
" </div>")
matches = re.finditer(regex, test_str, re.MULTILINE)
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