Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex to match comma separated aws regions in python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'(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}'

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading