Regex to extract from string

Posted on 2007-10-14
Medium Priority
Last Modified: 2008-01-09
I have the following

CN=Cost Admin Users,OU=Groups,OU=CCC,OU=ACP,OU=ABV,DC=Ant,DC=AAb,DC=Endy,DC=corl

I want to create a regex that will extract

Cost Admin Users

Question by:JonMny
LVL 96

Expert Comment

by:Bob Learned
ID: 20075645
You could do that pretty easy with most languages with simple string lookups, unless this isn't a language problem--which you never indicated either way.


Author Comment

ID: 20075650
Yeah I know just trying to learn reg ex so I wanted to see how this would be done
LVL 17

Accepted Solution

mreuring earned 2000 total points
ID: 20075795
Assuming it is a given that 'CN' is a unique identifier and a comma may not exist as part of the value, this would be a really simple expression to extract what you want:

However, how to use that expression is quite different per programming language. You'd have to test for a match and, when you get a match, grab group 1 from the match.
So, to make the programming a little less complicated you could make the regular expression a little more complicated:

This says to match anything that isn't a comma, as long as it is preceded by 'CN=', but 'CN=' won't be included in the match. This way the match will only contain the text you want.

Hope this helps,

LVL 63

Expert Comment

ID: 20076321
Put this in a file with the name cn.html and open it in browser:


var DN = "CN=Cost Admin Users,OU=Groups,OU=CCC,OU=ACP,OU=ABV,DC=Ant,DC=AAb,DC=Endy,DC=corl";

var CN = DN.match(/cn=([^,]+)/i)[1];




Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question