Home > Net >  Excluding a string name when it partially matches with any element of a list
Excluding a string name when it partially matches with any element of a list

Time:09-22

I am using Python3 to read AWS bucket names and specifically exit from the loop if the bucket name matches partially with any of the names defined in the bucket_exclusion_list array.

I created a bucket validation function as follows:

def valid_bucket(bucket_name):
    bucket_exclusion_list = ['do-not-delete', 'aws-config', 'elasticbeanstalk']
    for exclusion_items in bucket_exclusion_list:
        if bool(re.match(exclusion_items, bucket_name)) == True:
            return False
        else:
            continue
    return True

In my main function, I am calling the valid_bucket function and based on the boolean value, continuing furhter in the code.

if valid_bucket(bucket_name):
   bucket_info["bucketName"] = bucket_name
   bucket_info["bucketSize"] = ''
   ...

The code doesnt seem to be working as expected. I have to move on to the next bucket name if the current bucket name matches with the items in the exclusion list. Can someone help me understand the problem with the code.

CodePudding user response:

re.match will only match the beginning of the string. You can use in instead of a regex:

if exclusion_items in bucket_name:
    return False
else:
    continue

That being said, this function can be rewritten as:

def valid_bucket(bucket_name):
    bucket_exclusion_list = ['do-not-delete', 'aws-config', 'elasticbeanstalk']
    return all(item not in bucket_name for item in bucket_exclusion_list)

CodePudding user response:

Just change your function as below:

import re
def valid_bucket(bucket_name):
    bucket_exclusion_list = ['do-not-delete', 'aws-config', 'elasticbeanstalk']
    regex = "|".join(bucket_exclusion_list)
    if re.search(rf'{regex}', bucket_name):
        return True
    return False

if (valid_bucket(bucket_name)):
    print ("VALID")
else:
    print ("INVALID")

Output with bucket name = my-bucket

INVALID

Output with bucket name = do-not-delete my-bucket

VALID
  • Related