import re
regex = re.compile(r"\|poke\|p([1,2])\|(.+?)(,|\|)", flags=re.MULTILINE)
test_str = ("|poke|p1|Volcarona, M|\n"
"|poke|p1|Urshifu-*, F|\n"
"|poke|p1|Magnezone|\n"
"|poke|p1|Tangrowth, M|\n"
"|poke|p1|Corviknight, F|\n"
"|poke|p1|Hippowdon, F|\n"
"|poke|p2|Togekiss, M|\n"
"|poke|p2|Kommo-o, M|\n"
"|poke|p2|Jirachi|\n"
"|poke|p2|Mimikyu, F|\n"
"|poke|p2|Dragalge, F|\n"
"|poke|p1|Dragapult, F|\n"
"|poke|p1|Hatterene, F|\n"
"|poke|p1|Flygon, F|\n"
"|poke|p1|Inteleon, F|\n"
"|poke|p1|Obstagoon, F|\n"
"|poke|p1|Arcanine, F|\n"
"|poke|p2|Pincurchin, F|\n"
"|poke|p2|Magnezone|\n"
"|poke|p2|Hawlucha, F|\n"
"|poke|p2|Raichu-Alola, M|\n"
"|poke|p2|Ferrothorn, M|\n"
"|poke|p2|Magearna-Original|\n"
"|poke|p1|Slowbro, M|\n"
"|poke|p1|Tangrowth, F|\n"
"|poke|p1|Crawdaunt, F|\n"
"|poke|p1|Dragapult, M|\n"
"|poke|p1|Clefable, M|\n"
"|poke|p1|Excadrill, M|\n"
"|poke|p2|Togekiss, F|\n"
"|poke|p2|Starmie|\n"
"|poke|p2|Urshifu-*, M|\n"
"|poke|p2|Amoonguss, M|\n"
"|poke|p2|Cinderace, F|\n"
"|poke|p2|Jirachi|")
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