473,395 Members | 1,574 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,395 software developers and data experts.

make RE more cleaver to avoid inappropriate : sre_constants.error: redefinition of group name


I want to parse

'foo@bare' or '<foot@bar>' and get the email address foo@bar

the regex is

r'<\w+@\w+>|\w+@\w+'

now, I want to give it a name

r'<(?P<email>\w+@\w+)>|(?P<email>\w+@\w+)'

sre_constants.error: redefinition of group name 'email' as group 2;
was group 1

BUT because I use a | , I will get only one group named 'email' !

Any comment ?

PS: I know the solution for this case is to use r'(?P<lt><)?(?P<email>
\w+@\w+)(?(lt)>)'

Mar 29 '07 #1
5 3760
On Mar 29, 7:22 am, "aspineux" <aspin...@gmail.comwrote:
I want to parse

'foo@bare' or '<foot@bar>' and get the email address foo@bar

the regex is

r'<\w+@\w+>|\w+@\w+'

now, I want to give it a name

r'<(?P<email>\w+@\w+)>|(?P<email>\w+@\w+)'

sre_constants.error: redefinition of group name 'email' as group 2;
was group 1

BUT because I use a | , I will get only one group named 'email' !

Any comment ?

PS: I know the solution for this case is to use r'(?P<lt><)?(?P<email>
\w+@\w+)(?(lt)>)'


Regular expressions, alternation, named groups ... oh my!

It tends to get quite complex especially if you need
to reject cases where the string contains a left bracket
and not the right, or visa-versa.
>>pattern = re.compile(r'(?P<email><\w+@\w+>|(?<!<)\b\w+@\w+\b (?!>))')
for email in ('foo@bar' , '<foo@bar>', '<start@without_end_bracket'):
.... matched = pattern.search(email)
.... if matched is not None:
.... print matched.group('email')
....
foo@bar
<foo@bar>
I suggest you try some other solution (maybe pyparsing).

--
Hope this helps,
Steven

Mar 29 '07 #2
On 29 mar, 16:22, "aspineux" <aspin...@gmail.comwrote:
I want to parse

'foo@bare' or '<foot@bar>' and get the email address foo@bar

the regex is

r'<\w+@\w+>|\w+@\w+'

now, if I want to give it a name

r'<(?P<email>\w+@\w+)>|(?P<email>\w+@\w+)'

sre_constants.error: redefinition of group name 'email' as group 2;
was group 1

BUT because I use a | , I will get only one group named 'email' !
THEN my regex is meaningful, and the error is meaningless and
somrthing
should be change into 're'

But maybe I'm wrong ?
>
Any comment ?
I'm trying to start a discussion about something that can be improved
in 're',
not looking for a solution about email parsing :-)

>
PS: I know the solution for this case is to use r'(?P<lt><)?(?P<email>
\w+@\w+)(?(lt)>)'

Mar 29 '07 #3
On Mar 29, 3:22 pm, "aspineux" <aspin...@gmail.comwrote:
I want to parse

'foo@bare' or '<foot@bar>' and get the email address foo@bar

the regex is

r'<\w+@\w+>|\w+@\w+'

now, I want to give it a name

r'<(?P<email>\w+@\w+)>|(?P<email>\w+@\w+)'

sre_constants.error: redefinition of group name 'email' as group 2;
was group 1

BUT because I use a | , I will get only one group named 'email' !

Any comment ?

PS: I know the solution for this case is to use r'(?P<lt><)?(?P<email>
\w+@\w+)(?(lt)>)'
use two group names, one for each alternate form and if you are not
concerned with whichever matched do something like the following:
>>s1 = 'foo@bare'
s2 = '<foo@bare>'
matchobj = re.search(r'<(?P<email1>\w+@\w+)>|(?P<email2>\w+@\ w+)', s1)
matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
'foo@bare'
>>matchobj = re.search(r'<(?P<email1>\w+@\w+)>|(?P<email2>\w+@\ w+)', s2)
matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
'foo@bare'
>>>
- Paddy.

Mar 29 '07 #4
On 30 mar, 00:13, "Paddy" <paddy3...@googlemail.comwrote:
On Mar 29, 3:22 pm, "aspineux" <aspin...@gmail.comwrote:
I want to parse
'foo@bare' or '<foot@bar>' and get the email address foo@bar
the regex is
r'<\w+@\w+>|\w+@\w+'
now, I want to give it a name
r'<(?P<email>\w+@\w+)>|(?P<email>\w+@\w+)'
sre_constants.error: redefinition of group name 'email' as group 2;
was group 1
BUT because I use a | , I will get only one group named 'email' !
Any comment ?
PS: I know the solution for this case is to use r'(?P<lt><)?(?P<email>
\w+@\w+)(?(lt)>)'

use two group names, one for each alternate form and if you are not
concerned with whichever matched do something like the following:
The problem is the way I create this regex :-)

regex={}
regex['email']=r'(?P<email1>\w+@\w+)'

path=r'<%(email)s>|%(email)s' % regex

Once more, the original question is :
Is it normal to get an error when the same id used on both side of a
|
>
>s1 = 'foo@bare'
s2 = '<foo@bare>'
matchobj = re.search(r'<(?P<email1>\w+@\w+)>|(?P<email2>\w+@\ w+)', s1)
matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
'foo@bare'
>matchobj = re.search(r'<(?P<email1>\w+@\w+)>|(?P<email2>\w+@\ w+)', s2)
matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
'foo@bare'

- Paddy.

Mar 30 '07 #5
On Mar 30, 1:44 pm, "aspineux" <aspin...@gmail.comwrote:
On 30 mar, 00:13, "Paddy" <paddy3...@googlemail.comwrote:
On Mar 29, 3:22 pm, "aspineux" <aspin...@gmail.comwrote:
I want to parse
'foo@bare' or '<foot@bar>' and get the email address foo@bar
the regex is
r'<\w+@\w+>|\w+@\w+'
now, I want to give it a name
r'<(?P<email>\w+@\w+)>|(?P<email>\w+@\w+)'
sre_constants.error: redefinition of group name 'email' as group 2;
was group 1
BUT because I use a | , I will get only one group named 'email' !
Any comment ?
PS: I know the solution for this case is to use r'(?P<lt><)?(?P<email>
\w+@\w+)(?(lt)>)'
use two group names, one for each alternate form and if you are not
concerned with whichever matched do something like the following:

The problem is the way I create this regex :-)

regex={}
regex['email']=r'(?P<email1>\w+@\w+)'

path=r'<%(email)s>|%(email)s' % regex

Once more, the original question is :
Is it normal to get an error when the same id used on both side of a
|
>>s1 = 'foo@bare'
>>s2 = '<foo@bare>'
>>matchobj = re.search(r'<(?P<email1>\w+@\w+)>|(?P<email2>\w+@\ w+)', s1)
>>matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
'foo@bare'
>>matchobj = re.search(r'<(?P<email1>\w+@\w+)>|(?P<email2>\w+@\ w+)', s2)
>>matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
'foo@bare'
- Paddy.
Groups are numbered left-to-right irrespective of the expression
contents.
I am quite happy with the names being merely apseudonym for the
positional
group number and don't see a problem with not allowing multiple
occurrences of the same group name.
I did see some article about RE's and their speed. It seems that if
Pythons
RE package distinguished between 'grep style' RE' and the full set of
Python
RE's then their are much faster and efficient algorithms available for
the
grep style subset.

- Paddy.

Mar 30 '07 #6

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

Similar topics

7
by: Stefan | last post by:
Hi, I am trying to get my structure to be externed so I can use it in all my projects source files, but the code is generating errors. Here is some of my code: "Globals.cpp" typedef struct...
9
by: john hrdo | last post by:
Hey, This might well be a naive question, please be patient. Is it possible to compile a C program including two libraries lib1 and lib2 which have both a function with same name, e.g....
4
by: junaidnaseer | last post by:
Hi ! I am facing a problem that I have defined a function which when called in the same file generates an error as follows; " visual c error C2371 redefinition basic types see...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
5
by: vfunc | last post by:
Despite a #ifndef I am getting a redefinition error The offending .h file looks like the following #ifndef DISTRIB_H #define DISTRIB_H double dblpi; // this is getting compiled twice ...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
2
by: Mohammad Omer | last post by:
Hi, i am developing an application which uses WAB API's, for doing all this i am using vs2k5. I have wab.h header file included in my project to use WAB api's but after compilation one error...
12
by: tshad | last post by:
Does anyone have or know where I can get a some code that will check a TextBox for inappropriate language. At the moment, we need to manually check submissions for language before posting. This...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.