Regex to extract from string

Posted on 2007-10-14
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
    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.

    LVL 9

    Author Comment

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

    Accepted Solution

    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

    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];




    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    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…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now