python - How to remove everything after the last number in a string -
i have strings this:
w = 'w123 o456 t789-- --' my goal remove after last number, desired output be
w123 o456 t789 it not same ending, -- -- 1 example.
import re re.sub('(.*?)(\d)', '', w) gives me
'-- --' how can modify command removes part?
you can use:
>>> w = 'w123 o456 t789-- --' >>> re.sub(r'\d+$', '', w) 'w123 o456 t789' \d+$ remove 1 or more non-digit characters before end anchor $.
Comments
Post a Comment