user1387717 user1387717 - 6 months ago 18
Python Question

Python regex - Replace single quotes and brackets

I'd like to replace quantities with name then a square bracket and a single quote with the contents inside. So, from this:

RSQ(name['BAKD DK'], name['A DKJ'])


to this:

RSQ(BAKD DK, A DKJ)

Answer

Code -

import re
s = "RSQ(name['BAKD DK'], name['A DKJ'])"
expr = r"[\'\[\]]|\bname\b"
print(re.sub(expr, '', s))

Output -

RSQ(BAKD DK, A DKJ)