import re
regex = re.compile(r"^(counter|histogram)[.]_*[a-z]+[a-z_0-9]*[.]_*[a-z]+[a-z_0-9]*", flags=re.MULTILINE)
test_str = ("counter..\n"
"counter. . \n"
"counter.a.b\n"
"counter._._\n"
"counter._a._\n"
"counter._._a\n"
"counter._a._a\n"
"counter._1._1\n"
"counter._a1._a1\n"
"counter.system.crashes\n"
"counter.system_server.crashes\n"
"counter.system_server2.crashes_\n"
"counter.system_server2._crashes_\n"
"counter.system_server2._crashes_1\n"
"counter.system_server2._crashes_2\n"
"counter.2system_server.2crashes\n"
"counter.2system_server.crashes2\n"
"counter.system_server.crashes_2\n"
"counter.system_server._crashes_2\n"
"counter.system_server.2_crashes_2\n"
"counter.scheduler.jobs_delayed\n"
"counter.scheduler.jobs_delayed2\n"
"counter.scheduler_.jobs_delayed_\n"
"counter.scheduler._jobs_delayed_\n"
"counter.scheduler.jobs_delayed_\n"
"histogram.scheduler.jobs_delayed")
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