4
我正在使用Python(pl/python,实际上)在一个非常大的文本对象中连续查找一系列正则表达式匹配。这工作正常!每个匹配是一个不同的结果,每个替换将是一个不同的结果,最终基于循环内的查询。使用python finditer,我怎样才能替换每个匹配的字符串?
目前,我很乐意用任何文字替换rx中的每一个匹配,这样我就能理解它是如何工作的。有人能给我一个替换匹配文本的明确例子吗?
match.group(1)似乎正确指示匹配的文本;这是做事情的方式吗?
plan3 = plpy.prepare("SELECT field1,field2 FROM sometable WHERE indexfield = $1",
[ "text" ])
rx = re.finditer('LEFT[A-Z,a-z,:]+RIGHT)', data)
# above does find my n matches...
# ------------------- THE LOOP ----------------------------------
for match in rx:
# below does find the 6 match objects - good!
# match.group does return the text
plpy.notice("-- MATCH: ", match.group(1))
# must pull out a substring as the 'key' to an SQL find (a separate problem)
# (not sure how to split based on the colon:)
keyfield = (match.group(1).split, ':')
plpy.notice("---------: ",kefield)
try:
rv = plpy.execute(plan3, [ keyfield ], 1)
# --- REPLACE match.group(1) with results of query
# at this point, would be happy to replace with ANY STRING to test...
except:
plpy.error(traceback.format_exc())
# ------------------- (END LOOP) ------------------------------
伊格纳西奥 - OK,TKS这个 - 现在我想应用re.sub(),取得了一些成功。但是,我如何在匹配中搜索子字符串,以便抽出一些我需要的信息? – DrLou
该函数将传递[match object](http://docs.python.org/library/re.html#match-objects) –