fork download
  1. import re
  2.  
  3. def extract_values(sentence):
  4. num = r'\d*\.?\d+(?:/\d*\.?\d+)*'
  5. matches = re.findall(fr'(({num})(?:\s+(?:and|to)\s+({num}))*)', sentence)
  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. return result
  14.  
  15. # ['1 and 1/2', '.5', '5', '9 to 11', '9 to 9 and 1/2', '11/12/20']
  16. print(extract_values("He is 1 and 1/2 years old. He is .5 years old and he is 5 years old. He is between 9 to 11 or 9 to 9 and 1/2. He was born 11/12/20"))
  17.  
  18. text = "Score 1 and 2 sometimes, often 1 and 1/2, or 2.5 or 3 and 1/3."
  19. print( extract_values(text) )
  20.  
  21. # <1>, <2>, <1 and 1/2>, <2.5>, <3 and 1/3>
Success #stdin #stdout 0.02s 9416KB
stdin
Standard input is empty
stdout
['1 and 1/2', '.5', '5', '9', '11', '9 to 9 and 1/2', '11/12/20']
['1', '2', '1 and 1/2', '2.5', '3 and 1/3']