I'm learning REGEXes by Automate the Boring Stuff w / Python. In one of the titles in Chapter 7, the book teaches about character classes. Until then, easy. I created character classes for vowels ( re.compile(r'[aeiouAEIOU]')
), for letters and digits in intervals ( (r'a-zA-Z0-9')
) ... All quiet.
When I started learning about negative character classes, that is, defining a character class and having text strings detected, for example, in strings that do NOT have the character combination I defined by the character class, I started to find difficulties.
A negative character class is declared as: re.compile(r'[ˆaeiouAEIOU]')
, with the little hat on the front. But this is not making the character class negative: in fact, it is detecting vowels and the little hat, if you have little hat in the sentence.
See:
#Tentando (e conseguindo) detectar só VOGAIS...
>>> consonantRegex = re.compile(r'[aeiouAEIOU]')
>>> consonantRegex.findall('Robocop eats baby food. BABY FOOD')
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']
#Tentando detectar só CONSOANTES... (Perceba o chapeuzinho)
>>> consonantRegex = re.compile(r'[ˆaeiouAEIOU]')
>>> consonantRegex.findall('Robocop eats baby food. BABY FOOD')
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']
#Colocando um chapeuzinho na frase -> Chapeuzinho detectado
>>> consonantRegex.findall('Robocop eats baby food. ˆBABY FOOD.')
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'ˆ', 'A', 'O', 'O']
Data: