i don't understand this RE example from the documentation

J

John Salerno

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 '<[email protected]>' as well as
'(e-mail address removed)', but not with '<[email protected]'. New in version 2.4.

group(1) is the email address pattern, right? So why does the above RE
match '(e-mail address removed)'. If the email address exists, does the last part
of the RE: (?(1)>) mean that it has to end with a '>'?
 
J

John Salerno

John said:
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 '<[email protected]>' as well as
'(e-mail address removed)', but not with '<[email protected]'. New in version 2.4.

group(1) is the email address pattern, right? So why does the above RE
match '(e-mail address removed)'. 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.
 
B

Ben Cartwright

John said:
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.:
'bbbb'

And for the pattern you were looking at:
'(e-mail address removed)'

--Ben
 
J

John Salerno

Ben said:
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?
 

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
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top