How to get sequence of characters that are specific using Regex

Question:

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.

Asked By: BONE

||

Answers:

import re

a = " practicing_regular_expressions_z1_test_y3_test_y11_1930 "

re.findall(r"yd+", 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.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.