I'm trying to match all strings not ending with a specific word. For some reason the following doesn't work:
>> import re
>> my_str = 'static/assets/img/favicon.ico'
>> re.search('^static.+(?!ico)$', my_str)
<_sre.SRE_Match at 0x7f08b9773440>
Can you please explain why it's not working and how to fix it?
I think you want to use a negative lookbehind, not lookahead.
^static.+$(?<!ico)
See demo at regex101 if this is what you needed.
(?!ico)$ or $(?!ico) would look if there is not ico after the end which is impossible.
You should use:
print re.search(r'^(?!.*ico$)static.+', my_str)
None
(?!.*ico$) asserts that string doesn't end with ico
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With