import re
regex = re.compile(r"r", flags=re.MULTILINE)
test_str = ("Ex1. This is a random sentence.1,7,9 This is a sentence followed by it.\n"
"Output = This is a random sentence. 1,7,9 This is a sentence followed by it.`#space after the period will do.\n"
"Ex2. I love football.1,7,24`I also like cricket.\n"
"Output = I love football. 1,7,24`I also like cricket.\n\n"
"Ex3. ESD for undifferentiated cancers.[1][7]Cancers can be treata\n"
"ble.\n"
"Output = ESD for undifferentiated cancers. [1][7]Cancers can be treatable. #space after the period\n\n"
"EX4. |Age, n (%) | | |< | |\n"
"| | | |0.001 | |\n"
"| |> 65 years |641 (44.3) |28 (24.8) | |669 (42.9)|\n"
"| |? 65 years |806 (55.7) |85 (75.2) | |891 (57.1)|\n"
"# Tables should be untouched\n"
"EX5.75.6% vs. 54.0% # untouched\n\n"
"EX6. ask@to.in # should be untouched\n\n"
"EX7. Decimal numbers 22.3456 # should be untouched\n\n"
"EX8.")
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