re.split .
>>> string="hello there[my]friend"
>>> import re
>>> re.split('[] []', string)
['hello', 'there', 'my', 'friend']
In regex, [...]defines a character class. Any characters inside the brackets will match. The way I put the brackets avoids the need to avoid them, but the template also works [\[\] ].
>>> re.split('[\[\] ]', string)
['hello', 'there', 'my', 'friend']
The flag re.DEBUGfor re.compile is also useful as it prints to fit the pattern:
>>> re.compile('[] []', re.DEBUG)
in
literal 93
literal 32
literal 91
<_sre.SRE_Pattern object at 0x16b0850>
(where 32, 91, 93, - ascii values assigned , [, ])
source
share