I have strings like:
" 5473 & 4835 how to find the index"
" 5473 how to find the index"
" 5473 & A-121-2 how to find the index"
" A-121-2 how to find the index"
How can I extract the string up to last digit using regex.
For example, the result I want in above is like this:
" 5473 & 4835"
" 5473"
" 5473 & A-121-2"
" A-121-2"
Use greediness of *
^.*\d
^
asserts that we are at the start of a line. .*
will greedily matches all the characters upto the last \\d
and then it backtracks in-order to find a digit character. Example:
>>> re.search(r'^.*\d', " 5473 & 4835 how to find the index").group()
' 5473 & 4835'
>>> re.search(r'^.*\d', 'A-121-2 how to find the index').group()
'A-121-2'
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.