473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

i don't understand this RE example from the documentation

Ok, I've been staring at this and figuring it out for a while. I'm close
to getting it, but I'm confused by the examples:

(?(id/name)yes-pattern|no-pattern)
Will try to match with yes-pattern if the group with given id or name
exists, and with no-pattern if it doesn't. |no-pattern is optional and
can be omitted.

For example, (<)?(\w+@\w+(?:\.\w+)+)(?(1)>) is a poor email matching
pattern, which will match with '<us**@host.com>' as well as
'us**@host.com', but not with '<us**@host.com'. New in version 2.4.

group(1) is the email address pattern, right? So why does the above RE
match 'us**@host.com'. If the email address exists, does the last part
of the RE: (?(1)>) mean that it has to end with a '>'?
May 8 '06 #1
3 2433
John Salerno wrote:
Ok, I've been staring at this and figuring it out for a while. I'm close
to getting it, but I'm confused by the examples:

(?(id/name)yes-pattern|no-pattern)
Will try to match with yes-pattern if the group with given id or name
exists, and with no-pattern if it doesn't. |no-pattern is optional and
can be omitted.

For example, (<)?(\w+@\w+(?:\.\w+)+)(?(1)>) is a poor email matching
pattern, which will match with '<us**@host.com>' as well as
'us**@host.com', but not with '<us**@host.com'. New in version 2.4.

group(1) is the email address pattern, right? So why does the above RE
match 'us**@host.com'. If the email address exists, does the last part
of the RE: (?(1)>) mean that it has to end with a '>'?


I think I got it. The group(1) is referring to the opening '<', not the
email address. I had seen an earlier example that used group(0), so I
thought maybe the groups were 0-based.
May 8 '06 #2
John Salerno wrote:
John Salerno wrote:
Ok, I've been staring at this and figuring it out for a while. I'm close
to getting it, but I'm confused by the examples:

(?(id/name)yes-pattern|no-pattern)
Will try to match with yes-pattern if the group with given id or name
exists, and with no-pattern if it doesn't. |no-pattern is optional and
can be omitted.

For example, (<)?(\w+@\w+(?:\.\w+)+)(?(1)>) is a poor email matching
pattern, which will match with '<us**@host.com>' as well as
'us**@host.com', but not with '<us**@host.com'. New in version 2.4.

group(1) is the email address pattern, right? So why does the above RE
match 'us**@host.com'. If the email address exists, does the last part
of the RE: (?(1)>) mean that it has to end with a '>'?


I think I got it. The group(1) is referring to the opening '<', not the
email address. I had seen an earlier example that used group(0), so I
thought maybe the groups were 0-based.


The groups *are* 0-based. The 0th group is the whole match, e.g.:
import re
m = re.match(r'a(b+)', 'abbbb')
m.group(0) 'abbbb' m.group(1) 'bbbb'

And for the pattern you were looking at:
m = re.match(r'(<)?(\w+@\w+(?:\.\w+)+)(?(1)>)', '<fo*@test.com>')
m.group(0) '<fo*@test.com>' m.group(1) '<' m.group(2)

'fo*@test.com'

--Ben

May 8 '06 #3
Ben Cartwright wrote:
The groups *are* 0-based. The 0th group is the whole match, e.g.:


Ah, I see! But I also read that if a group number starts with 0, it is
treated as octal. Did I read that wrong, or maybe it doesn't matter
since 0 is 0 in any base system?
May 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
3
by: Robert | last post by:
Hello list, could somebody point me to a good reference about wxPython event handling? I have seen many examples but which one is the best. Waht are the advantages and disadvantages? Can you...
1
by: F. Petitjean | last post by:
Python has a very good support of unicode, utf8, encodings ... But I have some difficulties with the concepts and the vocabulary. The documentation is not bad, but for example in reading...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
17
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
19
by: LP | last post by:
I am using (trying to) CR version XI, cascading parameters feature works it asks user to enter params. But if page is resubmitted. It prompts for params again. I did set...
2
by: Volker Hetzer | last post by:
Hi! I've got a problem with the GC.keepalive function, as used in the examples at http://msdn2.microsoft.com/en-US/library/system.gc.keepalive.aspx .. There they have a class "Example",...
8
by: Scott M. | last post by:
Where will code that is preceded with: /// <example> /// some comments /// </example> actually show up? I can see my <summaryand <remarkscode showing up in the code comment pages and in...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.