朋友:在 PostgreSQL plpython 中,我试图在文本块“数据”中进行迭代搜索/替换。
使用 re-sub 定义匹配模式,然后调用函数“replace”来完成工作。目标是重复调用“替换”函数,因为一些替换会生成更多“规则”匹配,这需要进一步替换。
通过很多很多的替换,一切都很好——我正在设法触发重复循环的第二遍。然后,直到某些东西导致正则表达式模式返回一个整数(?)——显然在它找不到匹配的那一刻......??我试过测试“无”和“0”,但没有运气。想法?
data = (a_huge_block of_text)
# ====================== THE FUNCTION ==============
def replace(matchobj):
tag = matchobj.group(1)
plpy.info("-------- matchobj.group(1), tag: ", tag)
if matchobj.group(1) != '':
(do all the replacement work in here)
# ====================== END FUNCTION ==============
passnumber = 0
# If _any_ pattern match is found, process all of data for _all_ matches:
while re.search('(rule:[A-Za-z#]+)', data) != '':
# BEGIN repeat loop:
passnumber = passnumber + 1
plpy.info(' ================================ BEGIN PASS: ', passnumber)
data = re.sub('(rule:[A-Za-z#]+)', replace, data)
plpy.info(' =================================== END PASS: ', passnumber)
上面的代码似乎运行正常,进入第二次迭代......然后:
ERROR: TypeError: sequence item 21: expected string, int found
CONTEXT: Traceback (most recent call last):
PL/Python function "myfunction", line 201, in <module>
data = re.sub('(rule:[A-Za-z#]+)', replace, data)
PL/Python function "myfunction", line 150, in sub
PL/Python function "myfunction"
还尝试过 re.search (...) != '' -- 和 re.search (...) != 'None' --- 结果相同。我确实意识到我必须找到以某种可读形式表示匹配对象的语法......