Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex in for loop

Tags:

python

regex

How do you use a regex with a for loop in Python

 example data
 abc 1 xyz 0
 abc 2 xyz 1
 abc 3 xyz 2

How do you write regex for something like below

for i in range(1, 3):
     re.match(abc +i xyz +(i-1))
like image 956
user204488 Avatar asked Jun 18 '26 08:06

user204488


1 Answers

This substitutes i into the first %s and i-1 into the second %s

re.match("abc %s xyz %s"%(i,i-1), data)

another way to write it would be

re.match("abc "+str(i)+" xyz "+str(i-1), data)
like image 68
John La Rooy Avatar answered Jun 20 '26 21:06

John La Rooy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!