I need some advice to get my regular expression working as expected. It is an extended question to this question.
I want to replace all passwords and keys with "xxxx" and "zzzz".
import re
d = {"password": "xxxx", "secret-key": "zzzz"}
def replace(x):
key = x.group(1)
val = d.get(key, x.group(2))
return f"{key}={val}"
message = "test&password=secure password=secure and my secret-key=securekey should not be logged."
re.sub(r"(?<=???)(\S+)=(\S+)", replace, message)
Any number of characters and special characters can be placed in front of the key. In my example above, 'test&' is in front of the key "password" that I am looking for. But it is also possible that the key you are looking for starts after a whitespace (e.g. password=secure).
In the end this should come out of the above example.
message = test&password=xxxx password=xxxx and my secret-key=zzzz should not be logged.