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

Matching all occurrences with optional prefix/suffix

I have the following regex:

(\+|-|\^)?[a-z\d]+

I am trying to match any sequence of alphanumeric characters, that may or may not be preceded by a +, -, and may or may not be followed by a ^ and a series of digits. However, this does not produce the results that I want.

For example, the following code:

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

import re
r = re.findall(r'(\+|-|)?[a-z\d]+(\^\d+)?', '4x+5x-2445y^56')

Returns the result [('', ''), ('+', ''), ('-', '^56')], but I would expect it to return ['4x', '+5x', '-2445y^56'].

What am I doing wrong?

>Solution :

You are introducing two captured groups while trying to use optional ?, which will get returned by findall. You can make them non capture using ?: while still being able to group certain pattern together:

r = re.findall(r'[+-]?[a-z\d]+(?:\^\d+)?', '4x+5x-2445y^56')
r
['4x', '+5x', '-2445y^56']
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