ure_group.py 657 B

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