import re
regex = re.compile(r"path.+?(?:foo=(\d+))(?![a-zA-Z\d])|path((?!foo).)*$", flags=re.MULTILINE)
test_str = ("path?foo=67 \n"
"path?foo=67&bar=hello \n"
"path?bar=bye&foo=1&baz=12\n"
"path?bar=123\n"
"path\n"
"path?foo=37signals\n"
"path?foo=-8\n"
"something?foo=1\n"
"pathfoo\n")
subst = ""
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html