I have a few strings:
strings = [
'some.code(user, password="aA1$")',
'some.code("a1", passwd="aA1$", c=0)',
"some.code(user, passw='aA1$')\n",
]
I want to use python regex to replace all passwords with asterisks (*) like so:
some.code(username, password="****")
some.code("a1", passwd="****", c=0)
some.code(username, passw='****')
I got as far as this however I still have to use python string "replace" method:
pattern = r'\bpass\w+=[\'|\"](.+?)[\'|\"]'
for s in strings:
password = re.search(pattern, s).group(1)
print(s.replace(password, '*'*len(password)))
Output:
some.code(user, password="****")
some.code("a1", passwd="****", c=0)
some.code(user, passw='****')
Is there a way to achieve the same with a regex pattern?