import re
regex = re.compile(r"CIgcCursor.*<.*>\s*(\w*)[^}]*?(?:while|for)\s*\(.*?\n.*?(\{(?>[^{}]|(?-1))*\})")
test_str = ("Return_t\n"
"rgRoadNameChanges(Maneuver_o *pManeuverObj, rgManeuver_t *pManeuver, bool& bIsChanged)\n"
"{\n"
" Return_t Result;\n"
" Sint_t iIndex;\n"
" utSegment_t Seg;\n"
" utSegmentInfo_t SegInfo;\n"
" Uint_t nDist = 0;\n"
" DBID_t prevNavFeatureID;\n"
" DBID_t currDBID;\n"
" CIgcCursor_Sdal<DBNavFeatureAndAttrs_t> cursorFeature(true);\n\n"
" bIsChanged = false;\n"
" DBID_SET_NULL(prevNavFeatureID);\n"
" iIndex = pManeuver->RouteIndex;\n\n"
" while (nDist < 800)\n"
" {\n"
" if ((Result = utRouteGetSegment(pManeuverObj->hRoute, &Seg, iIndex)) == SDAL_FAILURE)\n"
" return NT_SUCCESS;\n\n"
" if ((Result = utSegmentGetInfo(&Seg, &SegInfo, NULL)) == SDAL_FAILURE)\n"
" return NT_TRACE(Result, NT_ERR_STR(\"rgRoadNameChanges: utSegmentGetInfo failed!\"));\n\n"
" if ((Result = utSegmentGetDBID(&Seg, &currDBID, NULL)) == SDAL_FAILURE)\n"
" return NT_TRACE(Result, NT_ERR_STR(\"rgRoadNameChanges: utSegmentGetDBID failed!\"));\n\n"
" Result = iDbGetSegNavFeatures(&currDBID, 0, cursorFeature);\n\n"
" if (Result > 0)\n"
" {\n"
" // Just comparing the first nav features for now. I don't know if we \n"
" // need to check all against the other list in case the order of the \n"
" // nav features changes. I'm assuming they'll stay the same order.\n"
" if (!(++cursorFeature).NoMoreRecords())\n"
" {\n"
" if (!DBID_IS_NULL(prevNavFeatureID)\n"
" && (iGuCompareDBIDsEqual(&prevNavFeatureID, &cursorFeature->navFeature.navFeatureID, GU_COMPARE_FULL_IDS) != 0))\n"
" {\n"
" bIsChanged = true;\n"
" break;\n"
" }\n"
" utMemcpy(&prevNavFeatureID, &cursorFeature->navFeature.navFeatureID, sizeof(DBID_t));\n"
" }\n"
" }\n\n"
" nDist += SegInfo.length;\n"
" iIndex++;\n"
" }\n"
"}")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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