using python finditer, how can I replace each matched string?
Asked Answered
Y

1

8

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 )  ------------------------------
Yellowthroat answered 20/6, 2011 at 1:53 Comment(0)
P
10

You want re.sub() instead.

import re

def repl(var):
  return var.group().encode('rot13')

print re.sub('[aeiou]', repl, 'yesterday')

yrstrrdny

Periotic answered 20/6, 2011 at 2:14 Comment(2)
Ignacio - OK, tks for this - I'm now trying re.sub(), with some success. But how do I substring search the match, to pull out a bit I need?Yellowthroat
The function will be passed the match objectPeriotic

© 2022 - 2024 — McMap. All rights reserved.