0

I have an array of strings I need to iterate over, push each string into a regex, then store it's output in a different array. I can get the regex to work fine if I enter 1 string at a time manually, but can't get it to loop over each string.

Here's what I have:

results = []
arr = ["CAT", "DOG", "BIRD"]

for i in arr:
    patt='(i)+'
    string= contents
    p=re.compile(patt)
    replen=[sp.end()-sp.start() for sp in p.finditer(string)]
    results.append(max(replen)/(len(patt)-3))
  1. string = contents -- I have a txt file that holds the string I'm performing the regex on. It's saved in contents. If I print(string) it properly outputs the txt file string

  2. What I'm trying to do is have the program look at the string stored in contents, then see how many times "CAT" is consecutively found (eg. ABCATCATCATDEFHGICAT = 3). I want to store that number in results, then do the same thing all over again with DOG and BIRD and so on.

  3. If I lose the for loop and manually enter CAT or DOG or whatever in patt='(i)+' everything works fine, but I need it to iterate over each entry in the array.

Minimum reproducible answer:

results = []
arr = ["CAT", "DOG", "BIRD"]

patt='(CAT)+'
string= "ABCATCATCATCATDEFIJKCAT"
p=re.compile(patt)
replen=[sp.end()-sp.start() for sp in p.finditer(string)]
results.append(max(replen)/(len(patt)-3))

The above should push 4 into the results array.

1
  • patt='(i)+' looks for the literal character i ... not for what the variable i contains. please provide a minimal reproducible example of your problem. Commented Oct 27, 2019 at 13:42

2 Answers 2

1

Use string formatting to construct the pattern.

arr = ["CAT", "DOG", "BIRD"]

for i in arr:
    patt = '({})+'.format(i)
    ...

Or with f-strings

arr = ["CAT", "DOG", "BIRD"]

for i in arr:
    patt = f'({i})+'
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

You're using the literal i instead of the variable i. If you're using python >= 3.6 you can use fstrings and quickly take care of this.

However you have another problem in your code, if the regex isn't matched even once, you're going to get that replen is None. I fixed it so it wouldn't be dependent on the length of the regex, and instead dependent on how many matches are found (which is what you want).

results = []
arr = ["CAT", "DOG", "BIRD"]

for i in arr:
    patt=f'({i})+'
    string= contents
    p=re.compile(patt)
    replen=len([sp for sp in p.finditer(string)])
    results.append(replen)

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.