I am trying to find all characters that are not c, i, k, m, o or v in a string. The regex pattern I am using at the moment is [abd-hjlnp-wx-z].
I was wondering if I could do something like [a-z AND not [cikmov]]. I am using python 2.7.
You can use a negative character class here. Start with \W (not a word); negated that means everything that is a word character, but you can then add all your exceptions:
[^\W_0-9cikmov]
[^...] is a negative character class, everything in the class must not match. With \W that means anything that is not in the ranges a-z, A-Z, 0-9 or and underscore would match, the ^ inverted that so we now match all letters, numbers and underscores.
To that we added numbers and an underscore, so now it only matches letters again. Then add your exceptions, and it'll only match all letters except c, i, k, m, o or v. The uppercase versions are still matched, unless you make the regex case insensitive.
Demo:
>>> import re
>>> re.findall(r'[^\W_0-9cikmov]', "Don't match 1232 or cikmov")
['D', 'n', 't', 'a', 't', 'h', 'r']
All letters were matched, except for your exceptions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With