import re
regex = re.compile(r"\d+(?:\.\d+)?(?:(?:x\d+(?:\.\d+)?)*)?(?=[ \t]+[cm]m)", flags=re.MULTILINE)
test_str = ("The study reveals a speculated nodule with pleural tagging at anterior basal segment of LLL, measured 1.9x1.4x2.0 cm in size\n"
"Other two ill defined, small ground glass lesions measured 4.0 mm and 3 mm in size, respectively.\n\n"
"The study reveals a speculated nodule with pleural tagging at anterior basal segment of LLL, measured 1.9x1.4x2.0 cm in size\n"
"Other two ill defined, small ground glass lesions measured 4.0 mm and 3 mm in size, respectively.")
subst = "\\1 [[replacement]]"
result = regex.sub(subst, test_str)
if result:
print(result)
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