What does re.compile (r '[[]]') match?

With the Python re module, why the following actions act differently:

>>> r = re.compile(r'[][]') >>> r.findall(r'[]') ['[', ']'] >>> r = re.compile(r'[[]]') >>> r.findall(r'[]') ['[]'] >>> r.findall(r'][') [] 
+4
source share
3 answers

The regular expression "[[]]" matches the substring "[]". The first [in the expression begins the character class, and the first] ends it. There is only one character ([) in the class, and then it must be second). Thus, the expression is "any of the characters in" ["followed by"] ".

+16
source

Character classes begin with [ and end first ] .

Thus, the expression [][] is a character class with characters ] and [ , since character classes should not be empty: [ ][ ]
And the expression [[]] is a character class with just [ and a single character ] after that: [ [ ] ]

+4
source

and r '[] []' forms the character class {'[', ']'} and matches either '[' or ']'.

0
source

All Articles