Python re.search simple question

Denis McMahon denismfmcmahon at gmail.com
Mon Dec 8 19:31:58 EST 2014


On Mon, 08 Dec 2014 12:22:37 +0530, Ganesh Pal wrote:

>>>> pattern
> 'Token-based migrations cannot be mixed with level-based: [prev 0 , now
> 1]'

Because [] defines a character class in a regex, if you want literal 
[ and ] you need to escape them with backslash.

[prev 0 , now 1] -> match any single character from the set "prev0,now1 "

\[prev 0 , now 1\] -> match the actual text [prev 0 , now 1]

Try these:

re.search('[prev 0 , now 1]','p') # matches (p in "prev0,now1 ")

re.search('[prev 0 , now 1]','x') # doesn't match (x not in "prev0,now1 ")

re.search('\[prev 0 , now 1\]','p') # doesn't match

re.search('\[prev 0 , now 1\]','[prev 0 , now 1]') # matches

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list