Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass a backreference to a function

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading