import re
regex = re.compile(r"^(?P<dst_ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?,?(?P<User>[a-zA-Z._\d]+),(?P<src_ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?<ID>\d+)", flags=re.MULTILINE)
test_str = ("OpenVPN CLIENT LIST\n"
"Updated,Thu Nov 11 10:00:15 2021\n"
"20.20.20.20,n.y.f_o_o,2.2.2.2:11111 Thu Nov 11 10:00:14 2021\n"
"10.10.10.10,a.a.foo,3.3.3.3:22222,Thu Nov 11 10:00:14 2021\n"
"Updated,Thu Nov 11 10:00:12 2021\n"
"Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
"30.30.30.30,t.p.bar,192.168.1.1:58348,Thu Nov 11 10:00:12 2021\n"
"40.40.40.40,test,192.168.1.11:58348,Thu Nov 11 10:00:02 2021\n"
"50.50.50.50,test_2,33.33.33.3:58348,Thu Nov 11 10:00:22 2021")
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