tags:

views:

269

answers:

4

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 would be "abcd".

Thanks.

A: 

Yeah, using perl/java syntax cn=([^,]*),. You'd then get the 1st group.

sblundy
+5  A: 
/^cn=([^,]+),/
shelfoo
+10  A: 
 /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 )

Kent Fredric
+1  A: 

Also, look for a pre-built LDAP parser.

Joel Coehoorn