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

Troubleshooting Python Regex Pattern

For a larger Python program I am developing, I am trying to write a method which removes all sections of a string eveloped by two different tags; a start tag r'{foo}' and an end tag r'{/foo}'. If it were to run successfully, it would take a string such as:

r'stay {foo}leave{/foo} stay {foo} leave {/foo} stay' 

and return string:

r'stay  stay  stay'. 

Furthermore, it wouldn’t do anything if the sections were incomplete. In other words, if you gave the program string:

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

r'stay {/foo} {foo} leave {/foo} {foo} stay'

it would return string:

r'stay {/foo}  {foo} stay'

which is the intended behavior.

To resolve this issue, I turned to the python re library to create a regular expression that would do this for me. The closest thing I’ve had to success is with the regex pattern r'{foo}.*{/foo}' which only works, if and only if, there is one tagged section within the string. For example, using the pattern r'{foo}.*{/foo}' with string:

r'stay {foo} leave {/foo} stay'

would return r'stay stay' as expected, but if I do the same with the first example:

r'stay {foo}leave{/foo} stay {foo} leave {/foo} stay'

I’d get r'stay stay' instead of the expected result r'stay stay stay'.
While I feel like I am so close to figuring this out, my understanding of regular expressions is far from advanced. I would appreciate some help troubleshooting the right regex pattern for this scenario.

>Solution :

Use the "non-greedy" (a.k.a. "minimal") version of the star operator, which is *?. Reference: https://docs.python.org/3/library/re.html#regular-expression-syntax

Hence, change your pattern to: r'{foo}.*?{/foo}'

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