Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression to parse LDAP dn

Tags:

regex

I have the following string:

cn=abcd,cn=groups,dc=domain,dc=com

Can a regular expression be used here to extract the string after the first cn= and before the first ,? In the example above the answer should be abcd.

like image 487
Eddie Awad Avatar asked Nov 29 '22 20:11

Eddie Awad


1 Answers

 /cn=([^,]+),/ 

most languages will extract the match as $1 or matches[1]

If you can't for some reason wield subscripts,

$x =~ s/^cn=//
$x =~ s/,.*$//

Thats a way to do it in 2 steps.

If you were parsing it out of a log with sed

sed -n -r '/cn=/s/^cn=([^,]+),.*$/\1/p'    < logfile > dumpfile 

will get you what you want. ( Extra commands added to only print matching lines )

like image 51
Kent Fredric Avatar answered Dec 20 '22 03:12

Kent Fredric