Regular expression to extract the common name from a DN
During a Sametime migration project, I needed to edit text files to perform the name change. In particular, I needed to extract the Common Name from a Notes Name to use as the new Displayname. In this case, I used Notepad++ : the challenge being to extract the “CN” from a name like this :
“CN=Tom Bosmans,OU=ORGUNIT,OU=BUSINESS,O=COMPANY”
In Notepad++, you can use regular expressions to search, and then use a macro to copy that value, and paste it at the end .
This is the regular expression you need to use, to extract from the previous example, Tom Bosmans , from “CN=Tom Bosmans,OU=ORGUNIT,OU=BUSINESS,O=COMPANY”
(?<=CN=)([^,]+)
This will work for LDAP DN’s (distinguished name) as well, eg ,
will extract
d000000001 from
"cn=d000000001,ou=orgunit,ou=users,o=company,C¾"
If your ldap’s primary key is uid, the reg ex will look like this :
(?<=uid=)([^,]+)
And of course, this regular expression should also work in java, python, bash scripts etc.