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

How to match a word surrounded by a prefix and suffix?

Is there any regex to extract words from text that are surrounded by a certain prefix and suffix?

Example:

test[az5]test[az6]test

I need to extract the numbers surrounded by the prefix [az and the suffix ].

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’m a bit advanced in Python, but not really familiar with regex.

The desired output is:

5
6

>Solution :

You are looking for the following regular expression:

>>> import re
>>> re.findall('\[az(\d+)\]', 'test[az5]test[az6]test')
['5', '6']
>>> 
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