Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82164

Re: Python re.search simple question

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: Python re.search simple question
Date 2014-12-09 00:31 +0000
Organization A noiseless patient Spider
Message-ID <m65ftu$hu3$1@dont-email.me> (permalink)
References <mailman.16710.1418021565.18130.python-list@python.org>

Show all headers | View raw


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@gmail.com

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Python re.search simple question Ganesh Pal <ganesh1pal@gmail.com> - 2014-12-08 12:22 +0530
  Re: Python re.search simple question Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-08 09:08 +0200
    Re: Python re.search simple question Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-08 09:13 +0200
  Re: Python re.search simple question Denis McMahon <denismfmcmahon@gmail.com> - 2014-12-09 00:31 +0000

csiph-web