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.
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.
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.
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.