In Python, I try to find the last position in an arbitrary string that does match a given pattern, which is specified as negative character set regex pattern. For example, with the string uiae1iuae200
, and the pattern of not being a number (regex pattern in Python for this would be [^0-9]
), I would need '8' (the last 'e' before the '200') as result.
What is the most pythonic way to achieve this?
As it's a little tricky to quickly find method documentation and the best suited method for something in the Python docs (due to method docs being somewhere in the middle of the corresponding page, like re.search()
in the re page), the best way I quickly found myself is using re.search()
- but the current form simply must be a suboptimal way of doing it:
import re
string = 'uiae1iuae200' # the string to investigate
len(string) - re.search(r'[^0-9]', string[::-1]).start()
I am not satisfied with this for two reasons:
- a) I need to reverse string
before using it with [::-1]
, and
- b) I also need to reverse the resulting position (subtracting it from len(string)
because of having reversed the string before.
There needs to be better ways for this, likely even with the result of re.search()
.
I am aware of re.search(...).end()
over .start()
, but re.search()
seems to split the results into groups, for which I did not quickly find a not-cumbersome way to apply it to the last matched group. Without specifying the group, .start()
, .end()
, etc, seem to always match the first group, which does not have the position information about the last match. However, selecting the group seems to at first require the return value to temporarily be saved in a variable (which prevents neat one-liners), as I would need to access both the information about selecting the last group and then to select .end()
from this group.
What's your pythonic solution to this? I would value being pythonic more than having the most optimized runtime.
Update
The solution should be functional also in corner cases, like 123
(no position that matches the regex), empty string, etc. It should not crash e.g. because of selecting the last index of an empty list. However, as even my ugly answer above in the question would need more than one line for this, I guess a one-liner might be impossible for this (simply because one needs to check the return value of re.search()
or re.finditer()
before handling it). I'll accept pythonic multi-line solutions to this answer for this reason.
e
matches[^0-9]
pattern. – Krol[^0-9]
. I'll update the question. – Lizbeths = 'uiae1iuae200aaaaaaaa'
return the index of last char before a digit akae
(8) or the last char akaa
(19)? – Lipoproteinuiae1iuae200aaaaaaaa
it should return the last position in the string, means19
. – Lizbeth