fork download
  1. import re
  2.  
  3. text = """cakes 10 are good.
  4. cakes 10c are good.
  5. cakes 20 21 22 are good.
  6. cakes 30, 31, 32 are good.
  7. cakes 40a, 40b, 40c are good."""
  8.  
  9. numerical = r"""\b[0-9]{1,4}(?:[.-]?[A-Za-z])?"""
  10.  
  11. # those could be separated by:
  12. separator = r""",?\s"""
  13.  
  14. # code to get the matches with finditer
  15. pattern2 = fr"""{numerical}(?:{separator}{numerical})*"""
  16.  
  17. refs = re.finditer(pattern2, text, re.VERBOSE)
  18. for element in refs:
  19. print(element.group())
Success #stdin #stdout 0.03s 9464KB
stdin
Standard input is empty
stdout
10
10c
20 21 22
30, 31, 32
40a, 40b, 40c