import re
regex = re.compile(r"(?<=Company\s)([\s\S]+?)[\n\r]+Is selling the.+[\n\r]+(.+)[\n\r]+.+[\n\r]+(\d+).+?(\S+@\S+)", flags=re.MULTILINE)
test_str = ("Company ABC\n\n"
"Is selling the following scrap material\n\n"
"EXCAVATION MACHINE\n\n"
", interested buyers can contact\n\n"
"17369999 or ABDULLA@CompanyABC.COM\n\n"
"Company GGG\n\n"
"Is selling the following scrap material\n\n"
"Coaster BUS\n\n"
", interested buyers can contact\n\n"
"17123123 or TTT@CompanyGGG.COM\n\n"
"Company DDD W.L.L\n\n"
"And partners\n\n"
"Is selling the following scrap material\n\n"
"Boats Motors\n\n"
", interested buyers can contact\n\n"
"175654556 or Jeff@CompanyDDD.COM\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