import re
regex = re.compile(r"^[\w.]+=\$\d+(?:\|[\w.]+=\$\d+)*$", flags=re.MULTILINE)
test_str = ("Valid Values:\n\n"
"ABC=$2|CDE=$1|Msg=$4|Ph.No=$3|TIME=$5\n\n"
"ABC=$2|CDE=$1|Msg123=$4|Ph.No=$3|TIME_23=$5\n\n"
"abc=$2|123=$1|cfg=$4|Ph.No=$3\n\n"
"ABC=$12|CDE=$1|Msg=$48|Ph.No=$3|TIME=$5\n\n"
"ABC=$22|CDE=$16|Msg123=$49|Ph.No=$3|TIME_23=$5\n\n"
"abc=$23|123=$51|cfg=$4|Ph.No=$03\n\n"
"Invalid Values:\n\n"
"ABC=$2CDE=$1Msg=$4\n\n"
"ABC=2|CDE=1|Msg123=$4|Ph.No=$3|TIME_23=$5\n\n"
"abc$2|123$1|cfg$4|Ph.No=$3\n\n"
"Msg123=$ |Ph.No=$ |TIME_23=5\n\n"
"abcdefgh|1234|eghjik\n\n"
"Msg123=$*|Ph.No=$()|TIME_23=$5\n\n"
"Msg123=$a|Ph.No=$b|TIME_23=$p")
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