fork download
  1. import re
  2.  
  3. def parse(text):
  4. if not text:
  5. return None
  6. match = re.search(r"([0-9]+(?:\.[0-9]+)?)\s*mm\b", text.lower())
  7. if match:
  8. return float(match.group(1))
  9. return text
  10.  
  11. tests = ['12.3 mm', '12.3mm', '32.0 mm / 1.259"', '32.0mm / 1.259"']
  12. for s in tests:
  13. print( parse(s) )
Success #stdin #stdout 0.02s 9512KB
stdin
Standard input is empty
stdout
12.3
12.3
32.0
32.0