fork download
  1. import re
  2. s = 'This is some temperature 30° c - 50 ° c 2°c 34.5 °c 30°c - 40 °c and "30° - 40, and -45.5° - -56.5° range 30c - 50 °c" or 30c - 40'
  3. tb = r'-?\d+(?:\.\d+)?(?:\s*°)?(?:\s*c)?'
  4. rx = r'{0}(?:\s*-\s*{0})?'.format(tb)
  5. results = re.findall(rx, s)
  6. print(results)
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
['30° c - 50 ° c', '2°c', '34.5 °c', '30°c - 40 °c', '30° - 40', '-45.5° - -56.5°', '30c - 50 °c', '30c - 40']