fork(7) download
  1. import re
  2.  
  3. string = """
  4. PIC S9(02)V9(05). I need this result "02 05"
  5. PIC S9(04). I need this result "04"
  6. PIC S9(03). I need this result "03"
  7. PIC S9(03)V9(03). I need this result "03 03"
  8. PIC S9(02)V9(03). I need this result "02 03"
  9. PIC S9(04). I need this result "04"
  10. PIC S9(13)V9(03). I need this result "13 03"
  11. """
  12. rx = re.compile(
  13. r"""
  14. \((\d+)\) # match digits in parentheses
  15. [^\n(]+ # match anything not a newline or another opening parenthesis
  16. (?:\((\d+)\))? # eventually match another group of digits in parentheses
  17. """, re.VERBOSE)
  18.  
  19. for match in re.finditer(rx, string):
  20. if match.group(2):
  21. m = ' '.join([match.group(1),match.group(2)])
  22. else:
  23. m = match.group(1)
  24. print m
  25.  
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
02 05
04
03
03 03
02 03
04
13 03