开发者

Extract name from LDAP path using regular expression

开发者 https://www.devze.com 2023-03-15 18:28 出处:网络
Currently I\'m using the regular expression below to extract names from the LDAP paths,开发者_如何学C it works fine until a comma has been used in the path.

Currently I'm using the regular expression below to extract names from the LDAP paths,开发者_如何学C 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


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]+=|$)


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


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}=|$)

0

精彩评论

暂无评论...
验证码 换一张
取 消