Exceptions within sets in Python regular expressions
You already know that special characters inside [] become regular characters. There are, however, exceptions: if you want square brackets as characters inside [ ] - you need to escape them with a backslash. For example, in the following code, the search pattern looks like this: there is a square bracket between the x's:
txt = 'x]x xax x[x x1x'
res = re.sub('x[\[\]]x', '!', txt)
print(res)
As a result, the following will be written to the variable:
'! xax ! x1x'
Given a string:
txt = 'x[]z x[[]]z x()z'
Write a regular expression that will find all words with the pattern: letter 'x', then square brackets any number of times, then letter 'z'.
Given a string:
txt = 'x[]z x{}z x.z x()z x([])z'
Write a regular expression that will find all words with the pattern: letter 'x', then any number of any brackets, then letter 'z'.