fork download
  1. import re
  2. numeral_rx = r'(?:zero|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand|million|billion|trillion)'
  3. sentences=["How much is twelve thousand three hundred four plus two hundred fifty six?",
  4. "How much is twelve thousand three hundred and four divided by two hundred fifty-six?"]
  5. pat_num = re.compile(r'\b{0}(?:(?:\s+(?:and\s+)?|-){0})*\b|\d+'.format(numeral_rx))
  6. for sentence in sentences:
  7. print(re.findall(pat_num, sentence))
Success #stdin #stdout 0.01s 23352KB
stdin
Standard input is empty
stdout
['twelve thousand three hundred four', 'two hundred fifty six']
['twelve thousand three hundred and four', 'two hundred fifty-six']