PowerShell: Recursive Regular Expression

Hello All,

I would like to know if it is possible to use Recursive Regular Expression Or if there is a regular expression that check for LDAP filter syntax.

To be more specific I am looking for a LDAP filter syntax regex in order to add it as a ValidatePattern Validation Attribute
like this:

Param
(
    [parameter(Mandatory=$true)]
    [ValidatePattern("(\s*\((?:[&|]\s*RECURSIVE+|(?:!\s*RECURSIVE)|[a-zA-Z][a-zA-Z0-9-]*[~]?=[^()]*)\s*\)\s*)")]
    [String[]]
    $LDAPFIlter
)

LDAP Syntax Filter
https://tools.ietf.org/search/rfc2254

I found this in perl:

/^(\s*((?:[&|]\s*(?1)+|(?:!\s*(?1))|[a-zA-Z][a-zA-Z0-9-][~]?=[^()])\s*)\s*)$/

ref: https://rgxdb.com/r/5VS2C5LM

But it is using recursive pattern (?1)+ . I think it is to get the correct amount of parenthesis.

I see that python as an addition to it that will allow the "recursive " parts.
https://pypi.org/project/regex/

Thank you in advance for your time.

Ref:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-6
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6#validatepattern-validation-attribute

Just wondering, why not sure a regular -filter, which is either powershell or sql syntax?

Hi js,
Yes thanks, I suppose that even if System.DirectoryServices.DirectorySearcher is used , the LDAP filter needs a specific format like “(&(objectCategory=User)(Department=IT))” or “(&(sAMAccountName<=a)(!(sAMAccountName=$*)))”

ref for AD : https://social.technet.microsoft.com/wiki/contents/articles/5392.active-directory-ldap-syntax-filters.aspx

The validation would be to confirm if the filter format is correct or not.

For SQL syntax , this is not possible unfortunately. This is not an AD. Just LDAP.