1import re
2
3def parse_wishlist(wishlists):
4 get_wishes = r"(?:\s*(?:[-*]\s*)?\s*)?[*-]\s?(.+)(?:\b|\B)"
5 pattern = r"\((\d+\.\d+)\)\s+(.+\b)\s+\[(\d+)(?:.+|\s*)\]"
6
7 wishlist = []
8 while wishlists:
9 match = re.search(pattern, wishlists)
10 person_data = wishlists[match.span()[0]:match.span()[1]]
11 behavior, name, age = re.findall(pattern, person_data)[0]
12 wishlists = wishlists[match.span()[1]:]
13
14 match = re.search(pattern, wishlists)
15 end = match.span()[0] if match != None else len(wishlists)
16 wishes = wishlists[:end]
17 wishlists = wishlists[end:]
18 wishlist.append((behavior, name, age, tuple(re.findall(get_wishes, wishes))))
19
20 return wishlist
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK