RE Question

Y

Yoav

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.
 
J

Jorge Godoy

Yoav said:
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,
 
Y

Yoav

Thanks, it seems like the first answer covers the second as well.

Thank you.

Jorge said:
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.
'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,
 
Y

Yoav

What is the difference between the RE module and the SRE one?

-------- Original Message --------
From: Jorge Godoy <[email protected]>
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.
'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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,312
Members
47,987
Latest member
Gaurav

Latest Threads

Top