I am using Python (pl/python, actually) to find, successively, a series of regex matches in a very large text object. This is working fine! Each match is a different result, and each replace will be a different result, eventually based on a query inside the loop.
For the moment, I'd be happy to replace every match in rx with any text, just so I'd understand how it works. Can someone give me an explicit example of replacing the matched text?
match.group(1) seems to correctly indicate the matched text; is this the way to do things?
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 ) ------------------------------