【发布时间】:2015-05-24 09:37:49
【问题描述】:
我无法用编辑的文本替换特定模式的特定出现。我已经使用了 for...loop 来提出如何获得特定出现的模式,但我无法将它与 re.sub 语句一起使用。
这是我的 Python 2 代码:
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
错误就行了:
content = re.sub(i,s,content)
因为变量“i”显然不是正则表达式。
错误:
TypeError: first argument must be string or compiled pattern
我该如何解决这个问题??
谢谢。
【问题讨论】:
-
你能添加实际的错误信息吗?
-
@ASGM 我添加了错误。
-
你认为
i是什么?你试过打印i吗? -
你的正则表达式中有很多反斜杠。你确定它是正确的吗?您能否举例说明您打算使用此代码实现的目标?
标签: python regex for-loop text replace