import re
regex = re.compile((r"(?xx) # all whitespaces are ignored in the pattern except between \Q and \E\n\n"
r" # Definition\n"
r" (?<item> # subpattern that forbids commas and allows balanced square brackets\n"
r" [^ ][, ]* (?: \[ \g<item> ] [^ ][, ]* )*+\n"
r" ){0}\n\n"
r" # Main pattern\n"
r" (?:\n"
r" \G (*negative_lookahead: \A ) , \n"
r" |\n"
r" \QnewProductsInfo: [\E\n"
r" (*:FIRST_ITEM) # add a marker to know when a new list starts\n"
r" (*SKIP) (*positive_lookahead: \g<item> (?: , \g<item> )*+ ] ) # check the syntax\n"
r" )\n"
r" \n"
r" \K # discard all that has been matched before from the match result\n"
r" \n"
r" \g<item> # a simple item followed by a comma\n"
r" (?: , \g<item> (*positive_lookahead: ] ) )? # eventual trailing item\n"))
test_str = ("newProductsInfo: [[1] - $2 dollars,[2] New Product,[3] Hello,[4]Same, [5]Value] , other things\n\n"
"newProductsInfo: [[$39 OFF] - Slytherin Snake Ear Cuffs - Low in Stock,FREE Mystery Box (Previously $30),The Jewelry Club: Exclusive VIP,[Exclusive Offer] / Discounted Mystery Box]")
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