Take the 2-minute tour ×

I want to extract 'James\, Brown' from the string below but I don't always know what the name will be. The comma is causing me some difficuly so what would you suggest to extract James\, Brown?

OU=James\, Brown,OU=Test,DC=Internal,DC=Net


share|improve this question
Too bad you can't get the API you're using to give you the output in a structured format. Parsing is tiresome. –  Jay Bazuzi Dec 10 '08 at 16:08
add comment

9 Answers

Assuming you're running in Windows, use PInvoke with DsUnquoteRdnValueW. For code, see my answer to another question: http://stackoverflow.com/a/11091804/628981

share|improve this answer
add comment

Replace \, with your own preferred magic string (perhaps & #44;), split on remaining commas or search til the first comma, then replace your magic string with a single comma.

i.e. Something like:

string originalStr = @"OU=James\, Brown,OU=Test,DC=Internal,DC=Net";
string replacedStr = originalStr.Replace("\,", "&#44;");

string name = replacedStr.Substring(0, replacedStr.IndexOf(","));
Console.WriteLine(name.Replace("&#44;", ","));
share|improve this answer
add comment

That looks suspiciously like an LDAP or Active Directory distinguished name formatted according to RFC 2253/4514.

Unless you're working with well known names and/or are okay with a fragile hackaround (like the regex solutions) - then you should start by reading the spec.

If you, like me, generally hate implementing code according to RFCs - then hope this guy did a better job following the spec than you would. At least he claims to be 2253 compliant.

share|improve this answer
add comment

I'd start off with a regex to split up the groups:

    Regex rx = new Regex(@"(?<!\\),");
    String test = "OU=James\\, Brown,OU=Test,DC=Internal,DC=Net";
    String[] segments = rx.Split(test);

But from there I would split up the parameters in the array by splitting them up manually, so that you don't have to use a regex that depends on more than the separator character used. Since this looks like an LDAP query, it might not matter if you always look at params[0], but there is a chance that the name might be set as "CN=". You can cover both cases by just reading the query like this:

    String name = segments[0].Split('=', 2)[1];
share|improve this answer
add comment

You can use a regex:

Match m = Regex.Match(input, "^OU=(.*?),OU=.*$");
share|improve this answer
add comment

If the slash is always there, I would look at potentially using RegEx to do the match, you can use a match group for the last and first names.


That RegEx will match names that include characters only, you will need to refine it a bit for better matching for the non-standard names. Here is a RegEx tester to help you along the way if you go this route.

share|improve this answer
add comment

If the format is always the same:

string line = GetStringFromWherever();

int start = line.IndexOf("=") + 1;//+1 to get start of name
int end = line.IndexOf("OU=",start) -1; //-1 to remove comma

string name = line.Substring(start, end - start);

Forgive if syntax is not quite right - from memory. Obviously this is not very robust and fails if the format ever changes.


share|improve this answer
Actually, the second parameter of SubString is length, not endIndex. In your example it SHOULD be name = line.SubString(start, end - start). I've always hated that about Substring, which is the reason why I've created extension methods that DO allow startIndex and endIndex. –  BFree Dec 10 '08 at 15:41
xan - I edited to correct syntax, since I am in front of a machine with Snippetcompiler installed. :) –  ZombieSheep Dec 10 '08 at 15:45
add comment

A regex is likely your best approach

static string ParseName(string arg) {
    var regex = new Regex(@"^OU=([a-zA-Z\\]+\,\s+[a-zA-Z\\]+)\,.*$");
    var match = regex.Match(arg);
    return match.Groups[1].Value;
share|improve this answer
A good approach, but one of which I have an irrational fear. :) –  ZombieSheep Dec 10 '08 at 15:46
Overcome your fear :) –  samjudson Dec 10 '08 at 15:48
But in order to do that, I must admit that my fear is wrong, and as a Yorkshireman, I am never wrong. ;-) –  ZombieSheep Dec 10 '08 at 15:49
You're assuming every name has a comma in it which might be wrong (and probably is). –  VVS Dec 10 '08 at 16:20
@David, questioner didn't mention it one way or the other so all I can go on is what they put in the question. I could also wonder if @'s are allowed in the name. Or perhaps 3 name vs. 2. But once again unless the asker puts it in their question assumptions are necessary. –  JaredPar Dec 10 '08 at 16:23
show 2 more comments

A quite brittle way to do this might be...

string[] splitUp = name.Split("=".ToCharArray(),3);
string namePart = splitUp[1].Replace(",OU","");

I wouldn't necessarily advocate this method, but I've just come back from a departmental Christmas lyunch and my brain is not fully engaged yet.

share|improve this answer
Hi, my name is "Foo,OUBar" but you can call me "FooBar" ;-) –  VVS Dec 10 '08 at 16:26
add comment

Your Answer

