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))
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With