Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I need to parse a string from this:

CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt

To this:

ES1-HER / ES1-Seura

Any easy way to do this with regex?

share|improve this question

closed as off-topic by Marius, legoscia, Prashant Kumar, Peter Schuetze, Maerlyn Nov 27 '13 at 15:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – legoscia, Prashant Kumar, Peter Schuetze, Maerlyn
If this question can be reworded to fit the rules in the help center, please edit the question.

    
At least try to solve the problem yourself first, this is not a resource that magically generates code for badly described problems. – nic Nov 27 '13 at 12:05
    
Why are you regexing LDAP strings? There's python modules that will parse that for you. – beerbajay Nov 27 '13 at 12:07
    
Hi, thanks for answers. I use a different python version embedded in a external program. – user2023042 Nov 27 '13 at 12:11
up vote 1 down vote accepted
>>> import re    
>>> s = 'CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt'
>>> re.findall('OU=([^,]+)', s)
['Computers', 'ES1-HER', 'ES1-Seura', 'RES-ES1']
>>> re.findall('OU=([^,]+)', s)[1:3]
['ES1-HER', 'ES1-Seura']
>>> ' / '.join(re.findall('OU=([^,]+)', s)[1:3])
'ES1-HER / ES1-Seura'

Don't use str as a variable name. It shadows builtin function str.

share|improve this answer
    
Nice, works great thanks – user2023042 Nov 27 '13 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.