2

I'm not much familiar with Regular expressions. I've a string from which I need to extract specific values using regex. Here is the string

CN=ReportingGroup {b4f3d644-9361-461e-b604-709fa31f2b9e},OU=DOM USERS,DC=domain,DC=com

I want to get the values of CN and OU namely "ReportingGroup {b4f3d644-9361-461e-b604-709fa31f2b9e}" and "DOM USERS "

from the above string. How can i construct the regex pattern for that?

4 Answers 4

7

You don't need a RegEx for this.

If you split the string using , and then each resulting string with =, you can look through the keys in order to extract the value for the CN and the OU keys.

string cn;
string ou;
foreach(string adPortion in myString.Split(new Char [] {','}))
{
   string[] kvp = adPortion.Split(new Char [] {'='})

   if(kvp[0] == "CN")
      cn = kvp[1];

   if(kvp[0] == "OU")
      ou = kvp[1];
}

This assumes that CN and OU only appear once in the string.

Sign up to request clarification or add additional context in comments.

5 Comments

I would advise against going this route. By performing manual splits, you would be effectively doing what Regex will do for you behind the scenes.
@Mr. TA - can you please expand on why a RegEx would be preferable?
Oded - Regexes were made specifically for the purpose of parsing strings. Your solution involves 12 lines of code, whereas with Regex, the whole thing fits into a one-liner. But line count aside, there's no reason at all to re-invent the wheel. Additionally, it's a good exercise for a beginner developer to learn Regex now that they have this relatively simple task to solve.
I would disagree with TA. Regex sometimes are an overkill for the application. IF you understand behind the scenes of regex (initializing a parser,scanner for tokens, building the parse tree etc....). But simple code like Oded will make it fast and makes much more sense.
I agree with @Oded here. Whilst the OP wanted a RegEx the provided solution is a perfectly acceptable alternative and easier to understand and therefore cheaper to maintain. When considering whether to use RegEx always remember the ancient proverb; "I used regular exressions to solve a problem. Now I have two problems."
5

Do the following:

new Regex("CN=(?<CN>[^,]*),OU=(?<OU>[^,]*)").Match(str).Groups["CN"].Value;

2 Comments

@ NLV See what I mean?, If you are not a Regex expert it is plain Chinese to non-chinese people :)
this is good too but won't agree it's faster than oded's solution.
1

It looks like your string is pretty well structured. Consider using regular string functions like IndexOf() and Substring(). Regex are harder to read and understand.

Comments

0

If you absolutely want to use Regex, the following code will iterate though all KEY=VALUE pairs:

        foreach (Match m in
            Regex.Matches(inputString, @"[,]*([^=]+)=([^,]*)"))
        {
            string key = m.Groups[1].Value;
            string value = m.Groups[2].Value;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.