JakeHolt
JakeHolt

Reputation: 31

Extract name from LDAP path using regular expression

Currently I'm using the regular expression below to extract names from the LDAP paths, it works fine until a comma has been used in the path.

Current regex:

CN=([^,]*).*

In the example LDAP path below I get "Deborah\" and I would like it to return "Deborah, James". I don't understand regular expressions and I have spent hours trying to make this work, can anyone help me solve this?

CN=Deborah\, James,OU=Staff,DC=Comp,DC=com

Much appreciated. Jake

Upvotes: 3

Views: 5194

Answers (3)

Daniel Martin
Daniel Martin

Reputation: 3

I tried the other answers listed here but they still returned the values outside of the common name. I didn't want a group so this is what I came up with. It will work if the CN inside a DN or OU.

(?i:(?<=CN=)).*?(?=,[A-Za-z]{0,2}=|$)

Upvotes: 0

user207421
user207421

Reputation: 310980

You should be using the LDAP API for parsing names, not regular expressions. In Java JNDI this means getting a NameParser from the Context.

Upvotes: 0

Samuel Neff
Samuel Neff

Reputation: 74919

If you're in a controlled environment and can control that CN will always be found by OU, then you can do use this:

CN=(.*),OU=

If you can't guarantee that then you can use this which is a little more complicated but will work if any other attribute follows CN or if CN is last:

CN=(.*?)(?:,[A-Z]+=|$)

Upvotes: 1

Related Questions