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

Merge the matches from regular expressions into a single list

I am trying to separate a string in CamelCase into a single list

I managed to separate the words with regular expressions

But I am clueless on how create a single list of all the matches

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

I tried to concatenate the lists, append something like that but I don’t think it would work in my case

n="SafaNeelHelloAByeSafaJasleen"
patt=re.compile(r'([A-Z][a-z]*|[a-z$])')
matches=patt.finditer(n)
for match in matches:
    a=match.group()
    list=a.split()
    print(list)

output:

['Safa']

['Neel']

['Hello']

['A']

['Bye']

['Safa']

['Jasleen']

Desired output:

['Safa','Neel','Hello','A','Bye','Safa','Jasleen']

>Solution :

You’re looking for re.findall(), not re.finditer():

>>> string = "SafaNeelHelloAByeSafaJasleen"
>>> pattern = re.compile(r"([A-Z][a-z]*|[a-z$])")
>>> pattern.findall(string)
['Safa', 'Neel', 'Hello', 'A', 'Bye', 'Safa', 'Jasleen']
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