1

I am attempting to retrieve the relative identifier (RID) from the security identifier (SID), which as I understand is the last sequence of numbers on the SID. I received a code example that uses regular expressions to retrieve the value, but I am new to RegEx (see link below). So, before I go using it without fully understanding it, my question is this: is there a reason to use RegEx as opposed to just getting the right most value before the last '-' in the string?

        // Retrieve the RID from the SID.
        string strObjectSid = "S-1-5-21-397955417-62688126-188441444-1010";
        var reg = new Regex( @"^S.*-(\d+)$" );
        var match = reg.Match(strObjectSid);
        var rid = match.Groups[1].Value;

The expected result for rid is 1010.

Active Directory: DirectoryEntry member list <> GroupPrincipal.GetMembers()

In addition, I tried retrieving the RID attribute from AD but it is not available. Microsoft does not provide any method for retrieving the RID. I know this may seem like a silly question, but this value is important for what I am trying to accomplish, so it needs to be correct along with whatever method I implement to retrieve it.

J Weezy
  • 3,507
  • 3
  • 32
  • 88

1 Answers1

2

There is no reason it can't be done without RegEx. I copied that from somewhere else too.

That regular expression just tells it to look for a grouping of digits after a dash that occurs just before the end of the string.

So, essentially, the last group of digits.

The same can be done with Substring (search for the last dash, add 1 and return everything from that position to the end):

strObjectSid.Substring(strObjectSid.LastIndexOf('-') + 1)
Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • 1
    Got it. Thank you for your help – J Weezy Apr 06 '18 at 16:49
  • @gagrielluci Can you help again? I have posted another AD question here https://stackoverflow.com/questions/49947133/active-directory-invalid-character-escaping – J Weezy Apr 20 '18 at 17:45