re.split question

Mitchell R Whorlow mrw065 at merle.acns.nwu.edu
Wed Jun 28 17:08:23 EDT 2000


Steffen Ries (steffen.ries at sympatico.ca) wrote:
...
> "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.)
...

How about using re.findall?

>>> s = r"""id=joe, ou=org\, unit, o=some org"""
>>> print s
id=joe, ou=org\, unit, o=some org
>>> patt = re.compile(r"""([^, ]+=[\w ]+(?:\\,)?[^,]+)""")
>>> patt.findall(s)
['id=joe', 'ou=org\\, unit', 'o=some org']

I think this does what you want, but taking into account the regex I used,
you still may prefer your solution.

Mitch



More information about the Python-list mailing list