import re
regex = re.compile(r"([\w]+)(?=\()", flags=re.MULTILINE)
test_str = ("+ perf_mmap__read_init(map, true, &start, &end);\n"
"+ while ((event = perf_mmap__read_event(map, true, &start, end)) != NULL) {\n"
"+ if(perf_mmap__read_init(md, opts->overwrite, &start, &end) < 0)\n"
"- set_pmd(pmd, pfn_pmd(__phys_to_pfn(phys), sect_prot));\n"
"--- a/arch/arm64/mm/hugetlbpage.c\n"
"+++ b/arch/arm64/mm/hugetlbpage.c\n"
"- ssize_t ret = device_store_int(s, attr, buf, size);\n"
"+ unsigned long old_check_interval = check_interval;\n"
"+ ssize_t ret = device_store_ulong(s, attr, buf, size);\n"
"+\n"
"+ if (check_interval == old_check_interval)\n"
"+ return ret;\n"
"+\n"
"+ if (check_interval < 1)\n"
"+ check_interval = 1;\n"
"+\n"
"+ mutex_lock(&mce_sysfs_mutex);\n"
"- mutex_unlock(&phydev->lock);\n"
"+ mutex_unlock(&mce_sysfs_mutex);\n"
"+static int __init dns323_parse_hex_nibble(char 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