Home > Enterprise >  How to pass a backreference to a function
How to pass a backreference to a function

Time:01-08

I have the following which works fine:

sql = 'SELECT id, date, instance_id FROM sales_1m'
print (re.sub(r'\b(%s)\b' % '|'.join(source.ns_mapping.keys()), r'\1', sql))
# SELECT id, date, instance_id FROM sales_1m

However, as soon as I try and pass the backreference to a function, it produces the following:

print (source.ns_mapping)
# {'data': '__SHADOW__test.data', 'test.data': '__SHADOW__test.data', 'data2': '__SHADOW__test.data2', 'test.data2': '__SHADOW__test.data2', 'sales_1m': '__SHADOW__test.sales_1m', 'test.sales_1m': '__SHADOW__test.sales_1m', 'season': '__SHADOW__test.season', 'test.season': '__SHADOW__test.season', 'team': '__SHADOW__test.team', 'test.team': '__SHADOW__test.team'}
print (re.sub(r'\b(%s)\b' % '|'.join(source.ns_mapping.keys()),
        source.ns_mapping[r'\1'], sql))

KeyError: '\1'

As if it invokes the function before capturing the backreference. How would I work around this?

My goals is to get this after substitution:

SELECT id, date, instance_id FROM __SHADOW__test.sales_1m

CodePudding user response:

The second argument of re.sub can take a function, so you could do this:

>>> re.sub(r'\b(%s)\b' % '|'.join(ns_mapping.keys()), lambda x: ns_mapping[x.group()], sql)
'SELECT id, date, instance_id FROM __SHADOW__test.sales_1m'
  •  Tags:  
  • Related