DoomerDGR8
DoomerDGR8

Reputation: 5042

Extract data value from a LDAP error message

I have an exception of kind DirectoryServicesCOMException and I need to extract the data value from it's ExtendedErrorMessage property.

Here is a sample test from the ExtendedErrorMessage property:

8009030C: LdapErr: DSID-0C0904DC, comment: AcceptSecurityContext error, data 701, v1db1

I need 701 from the string.

Just for reference, I found these messages from SO: https://stackoverflow.com/a/15024909/481656

I have success by using LastIndexOf 'data' and the next ',' after LastIndexOf 'data' combo but looking for a cleaner solution.

Thanks.

Upvotes: 0

Views: 343

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98810

If the result you want always before the last comma and it is only numbers, you can use String.Split(Char[], StringSplitOptions) overload with StringSplitOptions enumeration and Regex.Match(string, string) method like;

string s = "8009030C: LdapErr: DSID-0C0904DC, comment: AcceptSecurityContext error, data 701, v1db1";
string[] array = s.Split(new []{','},
                         StringSplitOptions.RemoveEmptyEntries); 
string s1 = array[array.Length - 2]; // This will be " data 701"
string finalstring = Regex.Match(s1, @"\d+").Value; // \d+ is for only integer numbers
Console.WriteLine(finalstring); // Prints 701

Here a demonstration.

Upvotes: 1

Related Questions