# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"intensity\"\s*:\s*\(\"\w+\"\s*:\s*(?<intensity>\d+)"
test_str = ("(\"data\": [\"from\" :\n"
"\"2024-04-25T11: 30Z\",\n"
"\"to\": \"2024-04-2512:00Z\",\n"
"\"intensity\": (\"forecast\": 152,\n"
"\"actual\": null, \"index\": \"moderate\"}), (\"from\": \"2024-04-25T12:002\",\n"
"\"intensity\": {\"forecast\": 152, \"actual\": null, \"index\": \"moderate\"}), (\"from\": \"2024-04-25T12:30Z\",\n"
"\"to\":\n"
"{\"from\": \"2024-04-25T13:00Z\", \"to\":\n"
"\"2024-04-25T12: 30Z\",\n"
"(\"forecast\": 164,\n"
"\"actual\": null,\n"
"\"2024-04-2513: 30Z\", \"intensity\": (\"forecast\": 154,\n"
"\"to\": \"2024-04-25T13: 002\",\n"
"\"intensity\": (\"forecast\": 154,\n"
"\"actual\": null,\n"
"\"index\":\n"
"\"actual\": null, \"index\": \"moderate\"}), (\"from\": \"2024-04-25T13:30Z*, \"to\": \"2024-04-25T14:002\",\n"
"\"moderate\"}},\n"
"\"intensity\":\n"
"04-25T14: 30Z\",\n"
"\"to\" :\n"
"\"index\": \"moderate\"3}, (\"from\": \"2024-04-25T14:002*, \"to\": \"2024-04-25T14:30Z\", \"intensity\": (\"forecast\": 166, \"actual\": null, \"index\": \"moderate\"}), (\"from\":\n"
"\" 2024-04-25T15:00Z\"\n"
"\"actual\": nu11,\n"
"\"index\"\n"
"\"intensity\": {\"forecast\": 170, \"actual\": null, \"index\": \"moderate\"}), {\"from\": \"2024-04-2515: 00Z\",\n"
"2024-\n"
"\"to\" :\n"
"\"moderate\"}), {\"from\": \"2024-04-25T15:30Z\", \"to\": \"2024-04-25T16:00Z\", \"intensity\": (\"forecast\": 175,\n"
"\"to\": \"2024-04-25T15: 30Z\",\n"
"\"intensity\": {\"forecast\": 172,\n"
"\"2024-04-25T16: 30Z\",\n"
"\"index\": \"moderate\"}}, (\"from\": \"2024-04-25T17:00Z\", \"to\":\n"
"\"intensity\": (\"forecast\": 177, \"actual\": null, \"index\": \"moderate\"?), (\"from\": \"2024-04-2516: 302\",\n"
"\"actual\" : nu11,\n"
"\"index\"\n"
"\"moderate\"}}, (\"from\": \"2024-04-2516: 00Z\",\n"
"\"to\": \"2024-04-25T17:002\",\n"
"\"intensity\": (\"forecast\": 179,\n"
"\"actual\": null,\n"
"2024-04-2517: 30Z\", \"intensity\": (\"forecast\": 181, \"actual\": null,\n"
"25T18:00Z\", \"intensity\": (\"forecast\": 184,\n"
"\"index\": \"moderate\"}}, {\"from\": \"2024-04-25T17: 30Z\",\n"
"\"actual\": null, \"index\": \"moderate\"}), (\"from\": \"2024-04-25T18:002\", \"to\": \"2024-04-25T18: 30Z\",\n"
"\"to\": \"2024-04-\n"
"\"moderate\"}}, (\"from\": \"2024-04-2518: 30Z\", \"to\": \"2024-04-25T19:002\",\n"
"\"intensity\": (\"forecast\": 187, \"actual\": null,\n"
"\"intensity\": (\"forecast\": 190,\n"
"\"actua1\": nul1,\n"
"\"index\":\n"
"\"index\":\n"
"\"high\"}}, (\"from\":\n"
"\"intensity\": {\"forecast\": 193,\n"
"\"actual\": null, \"index\":\n"
"\"2024-04-25T19: 00Z\", \"to\":\n"
"\"2024-04-25T19: 30Z\"\n"
"\"high\"}}, (\"from\": \"2024-04-25T19:30Z\", \"to\": \"2024-04-25T20:00Z\", \"intensity\":\n"
"{\"forecast\": 194,\n"
"\"2024-04-2520: 00Z\", \"to\": \"2024-04-25T20:30Z\", \"intensity\": {\"forecast\": 195, \"actual\": null, \"index\": \"high\"3}, (\"from\": \"2024-04-25T20:30Z\",\n"
"\"actual\": null, \"index\": \"high\")}, (\"from\":\n"
"\"2024-04-25T21:00Z\", \"intensity\": (\"forecast\":\n"
"198, \"actual\": null, \"index\": \"high\"'), (\"from\": \"2024-04-25T21: 002\",\n"
"\"2024-04-25T22: 00Z\", \"intensity\": {\"forecast\": 187, \"actual\": null,\n"
"\"to\": \"2024-04-25T21: 30Z\", \"intensity\": {\"forecast\": 196,\n"
"'actual\": null,\n"
"\"index\": \"high\"}}, {\"from\": \"2024-04-25T21:302\"\n"
"\"to\"\n"
"\"index\": \"moderate\"}}, (\"from\": \"2024-04-25T22:00Z\", \"to\":\n"
"\"2024-04-25T22: 30Z\",\n"
"\"intensity\": (\"forecast\": 181, \"actual\": null,\n"
"\"index\": \"moderate\"}}, {\"from\": \"2024-04-25T22:30Z\", \"to\": \"2024-04-25T23:002\", \"intensity\": (\"forecast\": 180, \"actual\": null,\n"
"\"index'\n"
"moderate\"}},{\"from\":\n"
"25T23:30Z\", \"intensity\": {\"forecast\": 172, \"actual\": null, \"index\": \"moderate\"}}, {\"from\": \"2024-04-25T23: 30Z\",\n"
"\"2024-04-25T23:002\",\n"
"\"to\":\n"
"\" 2024-04-\n"
"\"moderate\"}}, {\"from\": \"2024-04-26T00:00Z\", \"to\": \"2024-04-2600: 30Z\", \"intensity\": (\"forecast\": 150,\n"
"\"to\": \"2024-04-2600: 00Z\",\n"
"\"intensity\": (\"forecast\": 150,\n"
"\"actual\": null,\n"
"\"index\":\n"
"\"actual\": null, \"index\": \"moderate\")}, (\"from\": \"2024-04-26T00: 302\",\n"
"\"to\": \"2024-04-26T01:00Z\"\n"
"\"intensity\": {\"forecast\": 149,\n"
"\"actual\": null,\n"
"\"index\": \"moderate\"}}, (\"from\": \"2024-04-26T01:002\",\n"
"\"to\": \"2024-04-26T01:30Z\", \"intensity\": {\"forecast\": 149,\n"
"\"actual\": null,\n"
"\"index\":\n"
"\"moderate\"}}, ...")
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