C# Regex Code.

Hi,

I am trying to work out a regex that can split an OU path into is sub parts so I can create a comple path one OU at a time. (The bit I am worry about here is just how to split the path with regex).

OUPath = "LDAP://OU=Test2,oU=Test1,ou=LoadTestOU,DC=acme,DC=local";

What I would like to be able to do is change the code below

So it can split it like the following:

OU=Test2
OU=Test1
OU=LoadTestOU

I will also accept the output being

Test2
Test1
LoadTestOU

The main point is each OU is matched and the OU is not case sensitive. So all the following will match.

Ou=, OU=, oU=, ou=

So I know the code below does not work - wanted to change it so I can make it work.
I am looking for the modifcations required to make it work the way I want.

Thanks,

Ward



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace OUSplit
{
    class Program
    {
        static void Main(string[] args)
        {
            string OUPath;

            OUPath = "LDAP://OU=Test2,oU=Test1,ou=LoadTestOU,DC=acme,DC=local";

            Regex ex = new Regex("OU=(*?),",RegexOptions.IgnoreCase);
            MatchCollection col = ex.Matches(OUPath);
            List<String> values = new List<string>();

            foreach (Match matchItem in col)
            {
            }
        }
    }
}

Open in new window

LVL 1
whorsfallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lazyberezovskyCommented:
You can use following regex to match any part of path:
Regex.Match(OUPath, "OU=[^,]*")
Regex.Match(OUPath, "ou=[^,]*")

Also you can use groups to parse whole path:
Regex ex = new Regex("OU=(?<OU>[^,]*),oU=(?<oU>[^,]*),ou=(?<ou>[^,]*)");
Match match = ex.Match(OUPath);
// match.Groups["ou"].Value

Or you can simply split your path
OUPath.Substring(OUPath.LastIndexOf('/') + 1).Split(',')
0
lazyberezovskyCommented:
You can create method for retrieving path part.
GetPartValue(OUPath, "oU")

P.S. I know, that returning null is no good, but you can refactor this

static string GetPartValue(string path, string partName)
{
    Match match = Regex.Match(path, partName + "=(?<value>[^,]*)");
    return match.Success ? match.Groups["value"].Value  : null;
}

Open in new window

0
lucky_jamesCommented:
you can use =[^x] in place of OU=(*?),
this should give you output as

Test2
Test1
LoadTestOU
0
Pui_YunCommented:
Hi whorsfall,
Try the attached code.  It works for me.  Note: I'm using "(?i)" as the case in-sensitive modifier.

Here's a link to a good regex tutorial for .net applications:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx

Hope this helps!
P.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace OUSplit
{
    class Program
    {
        static void Main(string[] args)
        {
            string OUPath;

            OUPath = "LDAP://OU=Test2,oU=Test1,ou=LoadTestOU,DC=acme,DC=local";

            Regex ex = new Regex(@"(?i)(?<=[^a-z])ou=.*?(?=(?:,|\z))");
            MatchCollection col = ex.Matches(OUPath);
            List<String> values = new List<string>();

            foreach (Match matchItem in col)
            {
                Console.WriteLine(matchItem.Value);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

0
Shahan AyyubSenior Software EngineerCommented:
Use this one:
string OUPath;

            OUPath = "LDAP://OU=TeSt2,oU=Test1,ou=LoadTestOU,DC=acme,DC=local";

            Regex ex = new Regex("OU=[a-z]*\\d*", RegexOptions.IgnoreCase);
            MatchCollection col = ex.Matches(OUPath);
            List<String> values = new List<string>();

            foreach (Match matchItem in col)
            {
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.