import re
regex = re.compile(r"(lang=)[^&#]*")
test_str = ("http://winner.com/?page=home&lang=es&lang=en&lang=es&bas=2\n\n"
"http://winner.com/?page=home&lang=es&lang=en#hash\n\n"
"http://winner.com/?page=home&lang=es&lang=en&lang=es&base=2#hash\n\n"
"http://winner.com/test.html#HashValue?lang=QueryValue")
subst = "$1en"
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