469,336 Members | 5,592 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

RE Question

I don't understand why the two REs produce a different result. I read
the RE guide but still I can't seem to figure it out.
t 'echo user=name password=pass path="/ret files"\r\n' re.findall(r'(?<=\s)[^=]+=((?:".*")|(?:\S*))(?=\s)', t) ['name', 'pass', '"/ret files"'] re.findall(r'(?<=\s)[^=]+=((".*")|(\S*))(?=\s)', t)

[('name', '', 'name'), ('pass', '', 'pass'), ('"/ret files"', '"/ret
files"', '')]
Also, does '|' char (meaning or) produces a pair for each section. I
don't understand how it works. Can someone please direct me to a place
which will explain it?

Thanks.
Aug 18 '05 #1
3 1024
Yoav wrote:
I don't understand why the two REs produce a different result. I read
the RE guide but still I can't seem to figure it out.
>>> t 'echo user=name password=pass path="/ret files"\r\n' >>> re.findall(r'(?<=\s)[^=]+=((?:".*")|(?:\S*))(?=\s)', t) ['name', 'pass', '"/ret files"'] >>> re.findall(r'(?<=\s)[^=]+=((".*")|(\S*))(?=\s)', t)
[('name', '', 'name'), ('pass', '', 'pass'), ('"/ret files"', '"/ret
files"', '')]


Hi Yoav.

You can see at "sre" documentation (use that instead of the docs for "re")
that using "?:" you're asking for a non-groupping version of the
parenthesis match. When you use parenthesis, the matched expression is
saved for using later. Using "?:" you prevent that. This is what causes
the difference for you.
Also, does '|' char (meaning or) produces a pair for each section. I
don't understand how it works. Can someone please direct me to a place
which will explain it?


I didn't get your second question. When you use "|" the first match is
used. It is a short-circuited version of "or". I mean, it tries the first
regexp, if it matches, the second expression is ignored. The same is true
for "and", except that the comparisons end on the first false result.
Be seeing you,
--
Jorge Godoy <go***@ieee.org>

Aug 18 '05 #2
Thanks, it seems like the first answer covers the second as well.

Thank you.

Jorge Godoy wrote:
Yoav wrote:

I don't understand why the two REs produce a different result. I read
the RE guide but still I can't seem to figure it out.
>>> t

'echo user=name password=pass path="/ret files"\r\n'
>>> re.findall(r'(?<=\s)[^=]+=((?:".*")|(?:\S*))(?=\s)', t)

['name', 'pass', '"/ret files"']
>>> re.findall(r'(?<=\s)[^=]+=((".*")|(\S*))(?=\s)', t)

[('name', '', 'name'), ('pass', '', 'pass'), ('"/ret files"', '"/ret
files"', '')]

Hi Yoav.

You can see at "sre" documentation (use that instead of the docs for "re")
that using "?:" you're asking for a non-groupping version of the
parenthesis match. When you use parenthesis, the matched expression is
saved for using later. Using "?:" you prevent that. This is what causes
the difference for you.

Also, does '|' char (meaning or) produces a pair for each section. I
don't understand how it works. Can someone please direct me to a place
which will explain it?

I didn't get your second question. When you use "|" the first match is
used. It is a short-circuited version of "or". I mean, it tries the first
regexp, if it matches, the second expression is ignored. The same is true
for "and", except that the comparisons end on the first false result.
Be seeing you,

Aug 18 '05 #3
What is the difference between the RE module and the SRE one?

-------- Original Message --------
From: Jorge Godoy <go***@ieee.org>
To:
Subject: Re:RE Question
Date: 18/8/2005 17:44
Yoav wrote:

I don't understand why the two REs produce a different result. I read
the RE guide but still I can't seem to figure it out.
>>> t

'echo user=name password=pass path="/ret files"\r\n'
>>> re.findall(r'(?<=\s)[^=]+=((?:".*")|(?:\S*))(?=\s)', t)

['name', 'pass', '"/ret files"']
>>> re.findall(r'(?<=\s)[^=]+=((".*")|(\S*))(?=\s)', t)

[('name', '', 'name'), ('pass', '', 'pass'), ('"/ret files"', '"/ret
files"', '')]

Hi Yoav.

You can see at "sre" documentation (use that instead of the docs for "re")
that using "?:" you're asking for a non-groupping version of the
parenthesis match. When you use parenthesis, the matched expression is
saved for using later. Using "?:" you prevent that. This is what causes
the difference for you.

Also, does '|' char (meaning or) produces a pair for each section. I
don't understand how it works. Can someone please direct me to a place
which will explain it?

I didn't get your second question. When you use "|" the first match is
used. It is a short-circuited version of "or". I mean, it tries the first
regexp, if it matches, the second expression is ignored. The same is true
for "and", except that the comparisons end on the first false result.
Be seeing you,

Aug 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mohammed Mazid | last post: by
3 posts views Thread by Stevey | last post: by
10 posts views Thread by glenn | last post: by
53 posts views Thread by Jeff | last post: by
56 posts views Thread by spibou | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
3 posts views Thread by Zhang Weiwu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.