+ 3
Is it possible to use the difference between two characters set as one character set in regex?
Suppose I have a character set [ancde]. Now, I want to print the words of a sentence where the first character is a word character which is any word character except [ancde] and then the next two character to match is 'at' so if there is a sentence that has "aat dat vat cat bat tat dat fat fet ere tyy jat nat dat", The following pattern list will be ['vat', 'bat', 'tat', 'fat', 'jat'] I can do this by comparing with \W(isn't word char) https://code.sololearn.com/c85vPGc8HKy9 But I want something like we use in set {1,2,3,4,5}-{1,3,4}={2,5} So is it possible to directly use the difference of \w character and the char set [ancde]? Thank you and sorry for my bad English.
2 ответов
+ 3
You can fake a set intersection using positive lookahead.
r'(?=\w)[^ancde]at'
The bracket with ?= will check if the next character is a word character without actually consuming the character.
If the lookahead matches, the string will be matched against [^ancde] which does just what you would expect.
+ 3
I see .That's the thing I was looking for.Thank you very much @Schindlabua