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

Extract string between special characters in python

I would like to extract substrings from a string that is between special characters.

e.g a = "A1+A2*A3-A4"

Is there a way, I can get the output as A1,A2,A3,A4 if the operands list is ['*','/','+','-']
I know .split() works fine for a single operator but what would be the most optimal way to do it in a complicated expression ?

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

>Solution :

You can use re.split. you need to escape the characters as several have a special meaning in regex:

a = "A1+A2*A3-A4"
operators = ['*','/','+','-']

import re
re.split('|'.join(map(re.escape, operators)), a)

output: ['A1', 'A2', 'A3', 'A4']

Alternatively, if you only have single character operators, you can handcraft a character group:

re.split(r'[/*+-]', a)
alternatives

remove empty strings (anywhere):

a = "=A1+A2*A3-A4"
operators = ['=', '*','/','+','-']
l = list(filter(None, re.split('|'.join(map(re.escape, operators)), a)))

output: ['A1', 'A2', 'A3', 'A4']

remove empty string in the beginning:

a = "=A1+A2*A3-A4"
operators = ['=', '*','/','+','-']
l = re.split('|'.join(map(re.escape, operators)), a)
l = l[1:] if l and not bool(l[0]) else l

output: ['A1', 'A2', 'A3', 'A4']

keep the leading operator:

a = "=A1+A2*A3-A4"
operators = ['=', '*','/','+','-']
regex='(?!^)(?:%s)' % '|'.join(map(re.escape, operators))
l = re.split(regex, a)

output: ['=A1', 'A2', 'A3', 'A4']

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