fork download
  1. import re
  2.  
  3. text = "Score 1 and 2 sometimes, often 1 and 1/2, or 2.5 or 3 and 1/3."
  4.  
  5. matches = re.findall(r'((\d*\.?\d+(?:\/\d*\.?\d+)?)(?:\s+and\s+(\d*\.?\d+(?:\/\d*\.?\d+)?))?)', text)
  6.  
  7. result = []
  8. for x,y,z in matches:
  9. if '/' in x:
  10. result.append(x)
  11. else:
  12. result.extend(filter(lambda x: x!="", [y,z]))
  13.  
  14. print( result )
  15.  
  16. # <1>, <2>, <1 and 1/2>, <2.5>, <3 and 1/3>
Success #stdin #stdout 0.02s 9456KB
stdin
Standard input is empty
stdout
['1', '2', '1 and 1/2', '2.5', '3 and 1/3']