skip to Main Content

String has multiple data and need to find the match and get the value of comma separated aws regions

data = '''
appname=ivr
age=2years
region='us-east-1a,us-east-2a,us-east-1c'
'''

Would like to get the value for region in out put as 'us-east-1a,us-east-2a,us-east-1c'

tried using this regex but it works only for one value without comma

'(af|il|ap|ca|eu|me|sa|us|cn|us-gov|us-iso|us-isob)-(central|north|.       (north(?:east|west))|south|south(?:east|west)|east|west)-d{1}'

2

Answers


  1. You could just add an optional comma on the end of your regex and then make that a repeating group:

    pat = r'((?:af|il|ap|ca|eu|me|sa|us|cn|us-gov|us-iso|us-isob)-(?:central|north(?:east|west)?|south(?:east|west)?|east|west)-d+[a-z]?,?)+'
    
    out = re.search(pat, data)
    print(out.group())
    

    Output:

    us-east-1a,us-east-2a,us-east-1c
    
    Login or Signup to reply.
  2. A different approach is to use the csv library to parse:

    import csv
    
    data = """
    appname=ivr
    age=2years
    region='us-east-1a,us-east-2a,us-east-1c'
    """
    
    reader = csv.reader(data.strip().splitlines(), delimiter="=", quotechar="'")
    parsed = dict(reader)
    print(parsed)
    print(parsed['region'])
    

    Output:

    {'appname': 'ivr', 'age': '2years', 'region': 'us-east-1a,us-east-2a,us-east-1c'}
    us-east-1a,us-east-2a,us-east-1c
    

    Notes

    • The expression data.strip().splitlines() cleans up the data and split it into lines
    • We feed those lines into a csv.reader, specifying the equal sign as the fields delimiter.
    • Each line is broken into 2 fields, we then call dict() to turn them into a dictionary.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search