Reputation: 385
I am unable to replace a particular occurrence of a certain pattern with edited text. I have used a for...loop to come up with how to get a specific occurrence of the pattern but I am unable to use that with an re.sub statement.
This is my Python 2 code:
def split(content):
count = 0
s = ""
for i in re.finditer(r'(?s)(\\thinhline\n\\\\\[-16pt]\n)([a-zA-Z\s\(\)]+)(.*?)(\n *\\\\)', content):
count= count + 1
x = len(re.findall('^\s*&', i.group(3), re.M))
if x < 6:
break
elif x >= 6:
g = normalizationConstraints(i.group(3),i.group(2)) + "\n"
s = str(g) + "\n"
content = re.sub(i,s,content)
return content
The error is on the line:
content = re.sub(i,s,content)
Since the variable "i" is apparently not regex.
The error:
TypeError: first argument must be string or compiled pattern
How can I fix this problem??
Thanks.
Upvotes: 0
Views: 1447
Reputation: 11391
I'm not exactly sure what you want, but the error is telling you the right thing: i
isn't a regex pattern. re.finditer
returns an iterator of MatchObject
s, probably strings in this context. re.sub
expects a regular expression pattern as its first argument.
Depending on what you're trying to achieve with this code, you either need to use a different function than re.sub
, or you need to supply an actual regex. If you can give more details about your goals (maybe the expected output based on some sample input, as @200_success suggests), that would help.
Upvotes: 1