I am using Java's regular expression library.
I need to extract the common name from an active directory's distinguished name for a user. I have the String representation of a user's distinguished name. The only portion I need or care about is the common name. I am trying to write a regular expression that will extract that piece. I have some casual use of regular expressions, but nothing too in depth. I'm sure this is a pretty simple task, but the reluctant quantifier is what is hanging me up. The DN string looks like this:
CN=Some Name,OU=Staff,OU=Users,DC=somedomain,DC=com
I started with a pattern like this:
^CN=(.*),OU=.*$
which, of course, gives me:
Some Name,OU=Staff
my desired result is:
Some Name
I know the dot character class can be dangerous to use. But since the CNs can vary greatly, I wasn't sure how else to capture all the valid characters in the CN:
- whitespace (First Last)
- various punctuation (Last, First) or (Jim O'Reilly)
- numeric values (Suzie Smith2)
I tried a number of combinations of patterns that were aimed at making the ",OU=" portion of the pattern a reluctant match. But, I'm afraid my regex skills just aren't up to the task (yet anyway).
Any suggestions/help would be greatly appreciated.