import re
regex = re.compile(r"(st=(\w+))?(&?c=(\w+))?(&t=(\w+))?(&k=(\w+))?( HTTP)")
test_str = ("st=expand&c=22&t=button&k=fun HTTP\n"
"c=22&t=button&k=fun HTTP\n"
"st=expand&c=22&t=party&k=fun HTTP\n"
"st=expand&c=22&k=fun HTTP\n"
"st=expand HTTP\n"
" HTTP")
subst = "\\2,\\4,\\6,\\8"
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