regex to help with LDAP

J

jogdial

Hi, My Perl is very rusty and I'm having huge problems trying to pull
a common name out of a distinguished name.

The distinguished name(s) example:

"CN=KLAdmins,CN=Users,DC=contuso,DC=com"
"CN=EMLibrary Users,CN=Users,DC=contuso,DC=com"
"CN=Administrators,CN=Builtin,DC=contuso,DC=com"
"CN=Schema Admins,CN=Users,DC=contuso,DC=com"
"CN=Exchange Domain Servers,CN=Ussers,DC=contuso,DC=com"
"CN=Domain Users,CN=Users,DC=contuso,DC=com"
"CN=Enterprise Admins,CN=Users,DC=contuso,DC=com"
"CN=Group Policy Creator Owners,CN=Users,DC=contuso,DC=com"
"CN=Domain Admins,CN=Users,DC=contuso,DC=com"
"CN=Exchange Enterprise Servers,CN=Users,DC=contuso,DC=com"
"CN=Pre-Windows 2000 Compatible Access,CN=Builtin,DC=contuso,DC=com"
"CN=Users,CN=Builtin,DC=contuso,DC=com"

I just need the first CN value from this. My problem is that I don't
know how to deal with multiple words...

I want to stop on the first comma... but as anything that will match
anything is "greedy" I get all the way to the very last comma, rather
than the first if I try

/"CN=(.*),/

yet I don't konw how many words with spaces between them there are
going to be in the common name. Could be one, could be five or
anything in between.

Seems like it should be simple, but my brain is fried today, can
anyone please help me out with matching this?

Thanks
 
P

Peter Makholm

jogdial said:
I want to stop on the first comma... but as anything that will match
anything is "greedy" I get all the way to the very last comma, rather
than the first if I try

/"CN=(.*),/

Try reading the perlre documentation. Search for 'greedy' and see if
it helps.

//Makholm
 
J

Jens Thoms Toerring

jogdial said:
Hi, My Perl is very rusty and I'm having huge problems trying to pull
a common name out of a distinguished name.
The distinguished name(s) example:
"CN=KLAdmins,CN=Users,DC=contuso,DC=com"
"CN=EMLibrary Users,CN=Users,DC=contuso,DC=com"
"CN=Administrators,CN=Builtin,DC=contuso,DC=com"
"CN=Schema Admins,CN=Users,DC=contuso,DC=com"
"CN=Exchange Domain Servers,CN=Ussers,DC=contuso,DC=com"
"CN=Domain Users,CN=Users,DC=contuso,DC=com"
"CN=Enterprise Admins,CN=Users,DC=contuso,DC=com"
"CN=Group Policy Creator Owners,CN=Users,DC=contuso,DC=com"
"CN=Domain Admins,CN=Users,DC=contuso,DC=com"
"CN=Exchange Enterprise Servers,CN=Users,DC=contuso,DC=com"
"CN=Pre-Windows 2000 Compatible Access,CN=Builtin,DC=contuso,DC=com"
"CN=Users,CN=Builtin,DC=contuso,DC=com"
I just need the first CN value from this. My problem is that I don't
know how to deal with multiple words...
I want to stop on the first comma... but as anything that will match
anything is "greedy" I get all the way to the very last comma, rather
than the first if I try

yet I don't konw how many words with spaces between them there are
going to be in the common name. Could be one, could be five or
anything in between.
Seems like it should be simple, but my brain is fried today, can
anyone please help me out with matching this?

What about

/^"CN=([^,]*)/

i.e. accept all characters except a ',' (I did throw in the '^' at
the start since I suspect you only want to match at the start of a
line)?
Regards, Jens
 
M

Mirco Wahab

jogdial said:
I just need the first CN value from this. My problem is that I don't
know how to deal with multiple words...

I want to stop on the first comma... but as anything that will match
anything is "greedy" I get all the way to the very last comma, rather
than the first if I try

/"CN=(.*),/

yet I don't konw how many words with spaces between them there are
going to be in the common name. Could be one, could be five or
anything in between.

In addition to the regular solutions posted or mentioned,
you could, corresponding to your real intention, split
on the tags, like:

...
my @stuff = grep length, split/\n/,'
"CN=KLAdmins,CN=Users,DC=contuso,DC=com"
"CN=EMLibrary Users,CN=Users,DC=contuso,DC=com"
"CN=Administrators,CN=Builtin,DC=contuso,DC=com"
"CN=Schema Admins,CN=Users,DC=contuso,DC=com"
"CN=Exchange Domain Servers,CN=Ussers,DC=contuso,DC=com"
"CN=Domain Users,CN=Users,DC=contuso,DC=com"
"CN=Enterprise Admins,CN=Users,DC=contuso,DC=com"
"CN=Group Policy Creator Owners,CN=Users,DC=contuso,DC=com"
"CN=Domain Admins,CN=Users,DC=contuso,DC=com"
"CN=Exchange Enterprise Servers,CN=Users,DC=contuso,DC=com"
"CN=Pre-Windows 2000 Compatible Access,CN=Builtin,DC=contuso,DC=com"
"CN=Users,CN=Builtin,DC=contuso,DC=com"
';

print
+(split /[",][CD][CN]=/)[1] . "\n"
for @stuff;


...

The [1] after the split gives you the first elements (names),
the [2] the second elements and so on ...

Regards

Mirco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top