views:

100

answers:

2

hi how to use regex with for loop in python

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

how to write regex for something like below

for i in range(1, 3):
     re.match(abc +i xyz +(i-1))

Thanks in advance

+2  A: 

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)
gnibbler
thanks for the information
A: 

You can't make a single regex that includes math expressions which are evaluated at regex matching time. However, you can dynamically generate regex expressions, using the usual Python string formatting techniques:

import re

example_data = """
this line will not match
abc 1 xyz 0
this line will not match
abc 2 xyz 1
abc 2 xyz 2 will not match
abc 3 xyz 2
"""

for i in range(1, 4):
    pattern = "abc %d xyz %d" % (i, (i - 1))
    match_group = re.search(pattern, example_data)
    if match_group:
        print match_group.group(0)

This will print:

abc 1 xyz 0
abc 2 xyz 1
abc 3 xyz 2

It might be a better idea to do as abyx suggested, and make a single regex pattern with several match groups, and do the math based on the substrings captured by the match groups:

import re

example_data = """
this line will not match
abc 1 xyz 0
this line will not match
abc 2 xyz 1
abc 2 xyz 2 will not match
abc 3 xyz 2
"""
s_pattern = "abc (\d+) xyz (\d+)"

pat = re.compile(s_pattern)
# note that you can pre-compile the single pattern
# you cannot do that with the dynamic patterns

for match_group in re.finditer(pat, example_data):
    n1 = int(match_group.group(1))
    n2 = int(match_group.group(2))
    if n1 > 0 and n1 == n2 + 1:
        print match_group.group(0)

This also will print:

abc 1 xyz 0
abc 2 xyz 1
abc 3 xyz 2
steveha