Home > Mobile >  Regex match unless string is found
Regex match unless string is found

Time:07-12

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}

CodePudding user response:

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] }

CodePudding user response:

^\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 $.

  • Related