import re
regex = re.compile(r"(?P<assign_op>(\:|\!|\?)?=)|(?P<token_subst>$|%(\<\@))|(?P<token_ident>[\w\d\.\,]+)|(?P<target_name_match>%\.?)|(?P<token_rule_sep>[:])|(?P<incr_op>\n)|(?P<line_end>\n)|(?P<comment_op>#[.*]+$)|(?P<adjust_parser>[\$%\s\t\:])", flags=re.MULTILINE)
test_str = ("CC := nasm\n"
"BOOT_REQ := text.o\n"
"FREELDR_SRCS := freeldr.c\n"
"IMG_NAME := boot.img\n\n"
"TEXT_SECTION := 7c00\n\n"
"# UFS2, ZFS and/or RAW\n"
"FILESYSTEMS :=\n"
"CPU_FLAGS := PAE\n"
"SDK_FLAGS :=\n"
"VERSION := \"1.00.00\"\n\n"
"%.o:\n"
" $(ASM) ${SDK_FLAGS} -f bin -o $@ $(patsubst %.bin,%.asm,$@))\n\n"
"${IMG_NAME}:\n"
" $(BOOT_REQ) $(FREELDR_SRCS:.c=.o)\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