Inverting character sets in regular expressions
By using the hat '^' at the beginning of the square brackets, you can invert what you want. For example, if the command [ab] searches for the letter 'a' or 'b', then the command [^ab] will search for all characters except 'a' and 'b'.
Example
In this example, the search pattern looks like this: letter 'x', then NOT letter 'a', not 'b' and not 'c', then letter 'z':
txt = 'xaz xbz xcz xez'
res = re.sub('x[^abc]z', '!', txt)
print(res)
Result of code execution:
'xax xbx xcx !'
Example
In this example, the search pattern looks like this: letter 'x', then NOT a small Latin letter, then letter 'z':
txt = 'xaz xbz x1z xCz'
res = re.sub('x[^a-z]z', '!', txt)
print(res)
Result of code execution:
'xaz xbz ! !'
Practical tasks
Write a regular expression that will find strings with the pattern: digit 1, then a character that is not 'e' and not 'x', a digit 2.
Write a regular expression that will find strings with the pattern: letter 'x', then NOT a number from 2 to 7, letter 'z'.
Write a regular expression that will find lines with the pattern: letter 'x', then NOT a capital Latin letter from 1 and more times, letter 'z'.
Write a regular expression that will find strings with the pattern: letter 'x', then NOT a capital or small Latin letter and not a number from 1 to 5 from 1 and more times, letter 'z'.