1# test groups, and nested groups 2 3try: 4 import ure as re 5except ImportError: 6 try: 7 import re 8 except ImportError: 9 print("SKIP") 10 raise SystemExit 11 12 13def print_groups(match): 14 print("----") 15 try: 16 i = 0 17 while True: 18 print(match.group(i)) 19 i += 1 20 except IndexError: 21 pass 22 23 24m = re.match(r"(([0-9]*)([a-z]*)[0-9]*)", "1234hello567") 25print_groups(m) 26 27m = re.match(r"([0-9]*)(([a-z]*)([0-9]*))", "1234hello567") 28print_groups(m) 29 30# optional group that matches 31print_groups(re.match(r"(a)?b(c)", "abc")) 32 33# optional group that doesn't match 34print_groups(re.match(r"(a)?b(c)", "bc")) 35