• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

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

0
whorsfall
Asked:
whorsfall
1 Solution
 
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 Engineer - iOSCommented:
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now