I would like to remove all the digits from the end of: "Car 7 5 8 7 4". How can I achieve it using regex or other approaches? I tried following but it only deletes 1 digit:
re.sub(r'\s*\d+$', '', text)
Thanks
You can use
re.sub(r'\s*\d[\d\s]*$', '', text)
See the regex demo .
Details :
\s*
- zero or more whitespaces \d
- a digit [\d\s]*
- zero or more digits/whitespaces $
- end of string. You could use rstrip :
text.rstrip(' 0123456789')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.