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 get sequence of characters that are specific using Regex

I’m trying to get two separate regex matches of a string:

a = " practicing_regular_expressions_z1_test_y3_test_y11_1930 "

I need to get only "y3" and "y11" separately.

I’m a beginner and would appreciate any help.

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 :

import re

a = " practicing_regular_expressions_z1_test_y3_test_y11_1930 "

re.findall(r"y\d+", a)  # ['y3', 'y11']

This is a bit quick and dirty, but it works. The \d+ means 1 or more digits so it will capture both.

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