473,938 Members | 9,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2472
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
18164
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. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
3
2897
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 also have a short look at the example below and give me some comments, please? Example:
1
1422
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 http://docs.python.org/lib/module-unicodedata.html I had a long time to figure out what unicodedata.digit(unichr) would mean, a simple example is badly lacking. So I wrote the following script : #!/usr/bin/env python
25
7771
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 minutes with Python 3 times a week since, have 14 years of computing experience, 8 years in mathematical computing and 4 years in unix admin and perl, i have quickly found the official doc: http://python.org/doc/2.4.1/lib/module-gzip.html
17
2095
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 write a signature for the developer and after getting a good start developing a 2.0 application I thought I should go through the code and start marking classes and with namespaces. I keep getting the following 'missing' error even when using a first...
19
3897
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 ReuseParameterValuesOnRefresh="True" in a viewer, but it still doesn't work. Did anyone run into this problem. What's the solution? Please help. Thank you
2
1750
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", construct it and call a method, during which it may get collected. Why would it get collected, before the last method call ends? Is this a bug on the GC or is there a resson for it?
8
1901
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 the Object Browser, but where will the example come into play?
27
3880
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 interfaces. Like anything else, it appears Interfaces are by design something that requires documentation. I know this may be obvious, but if I have a class A and I say, well, class A implements IMyInterface. The fact that it implements...
0
10134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11524
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7381
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6295
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4904
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3501
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.