How can python regular expressions not return unmatched group

clipboard.png

use reg ="(? < = mileage). (re.findall: km | km (?! /) | (em: km | km (?! /). (? = mileage)"
the method is re.findall

the text above is input
the list below is output

for example, for the first one, I only need to take 34km
, for example, for the second one, I only need to take 60 kilometers

. How can

solve this problem at the level of regular expressions?


p = re.compile('()?.*?(?P<data>\d+(?:|km))(?(1).*?|.*?)')
print(p.search(line).group('data'))
Menu