I need to add “php” to all urls in href="xxx", that don’t end with “php”.
I use negetive lookahead (?!php):
find = r'href="(.+?)(?!php)"'
replace = r'href="\1.php"'
re.sub(find, replace, 'href="url"')
re.sub(find, replace, 'href="url.php"')
both add extension:
href="url.php"
href="url.php.php"
Why negative lookahead doesn’t work?
The following does work:
The reason your original regex
(.+?)(?!php)doesn’t quite work is that it matchesurl.phpas follows:(.+?)matchesurl.php;In other words,
.+?consumes the entire filename including the extension, making the lookahead a no-op.