Need to split this string using c#

SubbuUSA
SubbuUSA used Ask the Experts™
on
hello,

The string could have any number of OU Levels but I need to split and take only the string with OU Entries like this

The string will look like this..

(_path = "LDAP://CN=Test DealerUser1,OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,DC=qa-xt,DC=QA-Haworthinc,DC=com";)

after split i want result as (,OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,)

or

(OU=Test 09,OU=Dealers,OU=User-Accounts,DC=xt,DC=Haworthinc,DC=com) if it is given ..it should giave (OU=Test 09,OU=Dealers,OU=User-Accounts)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Your problem statement is a bit ambiguous.

Do you want () in the result?
Do you want the leading and trailing commas (you include them in one case but not the other)?
In the text after the "or", is the text with DC= an input or a desired output?

What are you saying after the "or" that is different from what you said before?

Author

Commented:
Dear Shadow77,

You  forget the statement after Or...sorry my bad

I want to split this string (_path = "LDAP://CN=Test DealerUser1,OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,DC=qa-xt,DC=QA-Haworthinc,DC=com";)

after split i want result as (OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts)

I hope i am clear now..sorry about that

You have to use the namespace using System.Text.RegularExpressions;
string _path = "LDAP://CN=Test DealerUser1,OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,DC=qa-xt,DC=QA-Haworthinc,DC=com";
Regex re = new Regex(@"(,OU=?.*,)+");
string []str = re.Split(_path);
MatchCollection mc = re.Matches(_path);
if(mc.Count >= 1)
{
	MessageBox.Show(mc[0].Value);
}

Open in new window

here is the updated one


using System.Text.RegularExpressions;
string _path = "LDAP://CN=Test DealerUser1,OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,DC=qa-xt,DC=QA-Haworthinc,DC=com";
Regex re = new Regex(@"(OU=?.*)+");
string []str = re.Split(_path);
MatchCollection mc = re.Matches(_path);
if(mc.Count >= 1)
{
	string output = string.Format("({0})", mc[0].Value);
	MessageBox.Show(output);
}

Open in new window

you can remove the line which says split. (string []str = re.Split(_path);)

Author

Commented:
Hello roshmon,

when i debug this is what i get..
 ?str[1]
",OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,DC=qa-xt,DC=QA-Haworthinc,"

I want only till
OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts

Commented:
This will do what you want:

string s = "(_path = \"LDAP://CN=Test DealerUser1,OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,DC=qa-xt,DC=QA-Haworthinc,DC=com\";)";
Regex re = new Regex(@"(,OU=[^,]+)+");
Match m = re.Match(s);
if (m.Success)
{
    string r = "(" + m.Value.Substring(1) + ")";
    string t = string.Format("Match result is \"{0}\".", r);
    MessageBox.Show(t);
}

Open in new window

sorry for the confusion, please ignore the previous comments which will include the DC part. The following code will solve those

using System.Text.RegularExpressions;
string _path = "LDAP://CN=Test DealerUser1,OU=Test Dealer 0001,OU=Dealers,OU=User-Accounts,DC=qa-xt,DC=QA-Haworthinc,DC=com";
Regex re = new Regex(@"(OU=.*?,)+");
MatchCollection mc = re.Matches(_path);
if(mc.Count >= 1)
{
	string output = string.Format("({0})", mc[0].Value.TrimEnd(','));
	MessageBox.Show(output);
}

Open in new window

Author

Commented:
Thank you so much

Commented:
@roshmon, @SubbuUSA:

It's better to put the comma at the beginning of the pattern, rather than the end.  If you have it at the end and there is nothing after the last OU term, that last OU term will be omitted from the match.  (See comment 33109149, above).
yes, that is a good suggestion. thanks :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial