# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"var\s(\w+)\s?\=\s?(\d+|\"[^\n\"]*\"|\'[^\n']*\')\s?\;"
test_str = ("<html>\n"
"<head>~~</head>\n"
"<body>\n"
" <div>contents</div>\n"
" <script>\n"
"var value1 = 55;var value2= 27;\n"
"var value3 = 'T';var value4 = \"FIT#$%SIZE\";\n"
"var value5 = '{\\\"P00000WJ000E\\\":{\\\"stock_price\\\":\\\"0.00\\\",\\\"use_stock\\\":true,\\\"use_soldout\\\":\\\"T\\\",\\\"is_display\\\":\\\"T\\\",\\\"is_selling\\\":\\\"T\\\",\\\"option_price\\\":79000,\\\"option_name\\\":\\\"FIT#$%SIZE\\\",\\\"option_value\\\":\\\"NOBLE-44\\\",\\\"stock_number\\\":26,\\\"option_value_orginal\\\":[\\\"NOBLE\\\",\\\"44\\\"],\\\"use_stock_original\\\":\\\"T\\\",\\\"use_soldout_original\\\":\\\"T\\\",\\\"use_soldout_today_delivery\\\":\\\"F\\\",\\\"is_auto_soldout\\\":\\\"F\\\",\\\"is_mandatory\\\":\\\"T\\\",\\\"option_id\\\":\\\"000E\\\",\\\"is_reserve_stat\\\":\\\"N\\\",\\\"item_image_file\\\":null,\\\"origin_option_added_price\\\":\\\"0.00\\\"}}';var value6 = '1';var value7 = 'string;must-catch';\n"
"var value8 = 8;\n"
"var value9 = 'S';\n"
"var value10 = 'T';\n"
" </script>\n"
"</body>\n"
"</html>")
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