fork download
  1. import re
  2. p = re.compile(r'(AA)(.*?)(ZZ)|(BB)(.*?)(YY)')
  3. test_str = "AA Text 1 here ZZ and BB Text2 there YY"
  4. print("Contents:")
  5. print([x.group(2).strip() for x in p.finditer(test_str) if x.group(2)])
  6. print([x.group(5).strip() for x in p.finditer(test_str) if x.group(5)])
  7. print("Delimiters:")
  8. print([(x.group(1), x.group(3)) for x in p.finditer(test_str) if x.group(1) and x.group(3)])
  9. print([(x.group(4), x.group(6)) for x in p.finditer(test_str) if x.group(4) and x.group(6)])
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
Contents:
['Text 1 here']
['Text2 there']
Delimiters:
[('AA', 'ZZ')]
[('BB', 'YY')]