Link to home
Start Free TrialLog in
Avatar of Dan Schimo
Dan SchimoFlag for United States of America

asked on

Regex get 8 digit alphanumeric from a string c#

Hello Experts,

I am trying to get the 8 digit Alphanumeric value from the below string. Please help..

rak45551
is string I am interested in.

CN=James Kera rak45551,OU=SOA,OU=End-Users,OU=KAL-Users,DC=KAL,DC=British,DC=com

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dan Schimo

ASKER

Sorry no Match yet....:(
 - Dan
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // First we see the input string.
        string input = "CN=James Kera rak45551,OU=SOA,OU=End-Users,OU=KAL-Users,DC=KAL,DC=British,DC=com";

        // Here we call Regex.Match.
        Match match = Regex.Match(input, @"content/(?:\s)(\w{8},)",
            RegexOptions.IgnoreCase);

        // Here we check the Match instance.
        if (match.Success)
        {
            // Finally, we get the Group value and display it.
            string key = match.Groups[1].Value;
            Console.WriteLine(key);
            
        }
        Console.Read();
    }
}

Open in new window

Hi Dan;

Try placing this line of code for the same in your code.
 
Match match = Regex.Match(input, @"CN=.*([a-zA-Z1-9]{8}),", RegexOptions.IgnoreCase);

Open in new window

Thank you Fernanado. I used Rgonzo1971 earlier and it worked. I was incorreclty calling an expression as string.
Very good.