re.split question

Darrell Gallion darrell at dorb.com
Tue Jun 27 00:39:53 EDT 2000


I find that often re.findall is easier to get right than re.split

>>> s="uid=joe, ou=org\\, unit, o=some org"
>>> re.findall("([^,]+)=([^,]+)", s)
[('uid', 'joe'), (' ou', 'org\\'), (' o', 'some org')]

--Darrell

----- Original Message ----- 
From: "Steffen Ries" <steffen.ries at sympatico.ca>


> Hi,
> 
> I'm trying use re.split to separate an LDAP Distinguished Name into
> its naming components. The naming components are of the form
> "attr=value" and they are separated by ",".
> 
> The part, which I can't solve is, that "value" may contain the
> sequence "\,", which obviously does not separate a naming component.
> 
> e.g.
> "uid=joe, ou=orgunit, o=some org"
> should be split into ["uid=joe", "ou=orgunit", "o=some org"]
> 
> "uid=joe, ou=org\\, unit, o=some org"
> -> ["uid=joe", "ou=org\\, unit", "o=some org"]
> 
> Is there a way to use re.split to achieve this? (I solved it searching
> [^\\], and splitting the string in a loop, but I was wondering if it
> can be done easier.)
> 






More information about the Python-list mailing list