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))
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
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.
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.
patt='(i)+'looks for the literal character i ... not for what the variableicontains. please provide a minimal reproducible example of your problem.