working with ldap files (2nd answer)

Tim Chase python.list at tim.thechases.com
Fri Sep 1 10:57:04 EDT 2006


>> I have this string on a field
>> CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com
>> this string is all the groups one user has membership.
>> So what I am trying to do.
>> read this string
>> and extract only the CNs
>>
>> like
>>
>> pointhairdepeoplethatsux,pointhairedboss
> 
>  >>> s = 
> "CN=pointhairedpeoplethatsux,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com;CN=pointhairedboss,OU=Groups,OU=Hatepeople,OU=HR,DC=fabrika,DC=com"

Or, if you're a regexp junkie...

 >>> import re
 >>> r = re.compile('CN=([^;,]*)')
 >>> r.findall(s)
['pointhairedpeoplethatsux', 'pointhairedboss']

I'll leave timing comparisons as an exercise to the reader... ;)

Both of these solutions make the assumption that neither a comma 
nor a semicolon are permissible in a CN.

-tkc






More information about the Python-list mailing list