3

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))
3
  • 1
    What is the problem with your example code (except its missing a closing parenthenis and some quotation marks)? Commented Nov 9, 2009 at 7:05
  • new to python and regex, there is problem with variable "i" where regex treating as letter. Commented Nov 9, 2009 at 7:15
  • Please post the regex you've tried. When you say "problem with variable 'i'", please post the error traceback and the regex with the problem. Please provide details of what's not working for you. Commented Nov 9, 2009 at 11:11

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.