import re
regex = re.compile(r"\bSOURCE#.*\s*^\[1](.*\s*^(?!\[\d+]).*\bPNR(?:\n(?!\[\d+]).*)*(?:\n\[\d+].*)*)", flags=re.MULTILINE)
test_str = ("**** SOURCE#24 ****\n\n"
"[1] Source Location [Local/Remote] : Remote\n\n"
" Remote Host Name : PNQ\n"
" User Name : foo\n\n"
"[2] Source directory : HDPGWRF\n"
"[3] Directory poll interval : 30\n"
"[4] File name format : ACR_FILEFORMAT\n"
"[5] Delete files from source : y\n\n"
"**** SOURCE#25 ****\n\n"
"[1] Source Location [Local/Remote] : Remote\n\n"
" Remote Host Name : PNR\n"
" User Name : foo\n\n"
"[2] Source directory : HDPGWRF\n"
"[3] Directory poll interval : 30\n"
"[4] File name format : ACR_FILEFORMAT\n"
"[5] Delete files from source : y\n\n"
"**** SOURCE#26 ****\n"
"etc.....")
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