0
votes

Can someone give me a hand in creating a regex string to match the first 3 entries, but omit the one that includes "_Classes".

Sample data set

S-1-5-21-1562028002-2160284861-498729489-2544
S-1-5-21-1562028002-2160284861-498729489-5555
S-1-5-21-1562028002-5623562356-895838383-7777
S-1-5-21-1562028002-5623562356-895838383-7777_Classes

This seems to match everything. How could it be modified to not match the similar string which includes "_Classes" ?

\D\-\d{1}\-\d{1}\-\d{2}\-\d{10}\-\d{10}\-\d{9}\-\d{4}
2
Add $ to the end of your regex.PM 77-1
Why not just use | Where-Object { $_ -as [System.Security.Principal.SecurityIdentifier] } instead of regex ?Santiago Squarzon
Or if this is going to be in a larger string just put \b at the end of your regex there.TheMadTechnician
Another way is to use a negative lookahead: regex101.com/r/4ujHsK/1zett42
@PM 77-1 for the Win. Wow I struggled with this a while. Just add a $, so simple.user11221802

2 Answers

2
votes

I would personally not use and leverage the SecurityIdentifier Class that can successfully parse your strings, the -as Type Operator also helps in this operation:

The -as operator tries to convert the input object to the specified .NET type. If it succeeds, it returns the converted object. It if fails, it returns $null. It does not return an error.

@'
S-1-5-21-1562028002-2160284861-498729489-2544
S-1-5-21-1562028002-2160284861-498729489-5555
S-1-5-21-1562028002-5623562356-895838383-7777
S-1-5-21-1562028002-5623562356-895838383-7777_Classes
'@ -split '\r?\n' | Where-Object { $_ -as [System.Security.Principal.SecurityIdentifier] }
1
votes
^\D-\d{1}-\d{1}-\d{2}-\d{10}-\d{10}-\d{9}-\d{4}$

The ^ and $ are added to force the string being matched to start and end at exactly those points. Since _Classes would cause the string to end further ahead, it will no longer be matched thanks to the $.