Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82109 > unrolled thread
| Started by | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| First post | 2014-12-08 12:22 +0530 |
| Last post | 2014-12-09 00:31 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| Date | 2014-12-08 12:22 +0530 |
| Subject | Python re.search simple question |
| Message-ID | <mailman.16710.1418021565.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
Hi Folks ,
This might seem to be very trivial question but iam breaking my head over
it for a while .
My understanding is that re.search should search for the match anywhere in
the string .
why is re.search failing in the below case ??
>>> pattern
'Token-based migrations cannot be mixed with level-based: [prev 0 , now 1]'
>>> text
' LogMessage {type NDMP_LOG_DBG} {msg_id 0} {msg The process id for NDMP
service is 0x9c216370} {associated_msg 0} {associated_msg_seq 0} Source
filer:DartOS Error: Token-based migrations cannot be mixed with
level-based: [prev 0 , now 1]'
>>> if (re.search(pattern,text)):
... print "Hi"
... else:
... print "BYE"
...
BYE
Regards,
Ganesh
[toc] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2014-12-08 09:08 +0200 |
| Message-ID | <qotsigq39g4.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #82109 |
Ganesh Pal writes: > why is re.search failing in the below case ?? Your pattern, '... level-based: [prev 0 , now 1]', matches a literal string '--- level-based: ' followed by 'p', 'r', 'e', 'v', ' ', '0', ..., or '1', none of which is the '[' found in your text at that position. Are you sure you need a regexp? If you are, make your pattern r'... \[prev 0 , now 1\]'. The 'r' is not a typo.
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2014-12-08 09:13 +0200 |
| Message-ID | <qotoare396y.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #82110 |
Jussi Piitulainen writes: > Ganesh Pal writes: > > > why is re.search failing in the below case ?? > > Your pattern, '... level-based: [prev 0 , now 1]', matches a literal > string '--- level-based: ' followed by 'p', 'r', 'e', 'v', ' ', '0', > ..., or '1', none of which is the '[' found in your text at that > position. > > Are you sure you need a regexp? > > If you are, make your pattern r'... \[prev 0 , now 1\]'. The 'r' is > not a typo. On the other hand, the '---' *is* a typo, meant to be an ellipsis where I shortened your text. The shortened pattern would still match it, but that's an accident: '.' has a special meaning in a regexp. Sorry about that.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-12-09 00:31 +0000 |
| Message-ID | <m65ftu$hu3$1@dont-email.me> |
| In reply to | #82109 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web