Regular expression matches multiple words in any order

I am writing a python script that will flag the output for a Windows CIS test. To do this, I match the values ​​in Group Policy settings against a regular expression to see if they meet the criteria for the reference.

Some aspects of the reference require the user list to be present in the setup, but it must be exclusive and the order specified must not be consistent.

As an example, "Configure memory quotas for a process" should be Administrators,LOCAL SERVICE,NETWORK SERVICE

, however it can also be represented as LOCAL SERVICE,NETWORK SERVICE, Administrators

, but it cannot be Administrators,LOCAL SERVICE,NETWORK SERVICE,phil

.

I am looking for a regex that can match these required values ​​in any order like this , but only fits if there is no other value.

Thanks Phil

Edit: This is not the same as Regex matching a string containing two names in any order and Multiple words in any order using regex , as they do not match words exclusively. I'm looking to only match the requirement names, but in any order.

Second edit: the script loads a set of rules from a csv file that contains the comparison item number, description, required value, and regex to match Group Policy settings. The idea was that we would be able to create a csv with rules for any of the tests, and the script shouldn't have to know if parameters should be numeric, username lists, booleans, etc.

The rules are loaded from the csv into a list (test case in the example below) and the policy settings are loaded from the CA into the second list (policy). This allows me to keep validating values ​​as agnostic as possible from the point of view of which the benchmark is being used.

for row in benchmark:
    if re.match(row[4],policy[row[2]]):
        continue
    print('"{}","{}","{}","{}"'.format(row[0],row[1],policy[row[2]],row[3]))

      

Example line in a csv test:

"2.2.5","Ensure 'Adjust memory quotas for a process' is set to 'Administrators,LOCAL SERVICE,NETWORK SERVICE'","Adjust memory quotas for a process","Administrators, LOCAL SERVICE, NETWORK SERVICE","<insert regex here>"

      

The final output is written to a csv file (or printed in csv format here) if the requirement for the reference does not match the format item number, item description, current value, required value

+3


source to share


1 answer


What about

^[(Administrators)(LOCAL SERVICE)(NETWORK SERVICE),\s]+$

      

or



^(Administrators|LOCAL SERVICE|NETWORK SERVICE|[,\s])+$

      

see work https://regex101.com/r/oJF0aW/2/tests

both versions basically say that the entire string should only contain the specified usernames, as well as commas and spaces.

+3


source







All Articles