0

I'm making regex based search engine.

For example if a query matches (?P<filename>\d+@\d+) then I want to run the query {"table": "records", "filter": {"filename": "*filename"}}. But before doing so I want to replace "*filename" with say "2786@20150510201045".

So basically I want to loop through a nested dictionary and replace anything starting with * with the value from the corresponding regex named group. The closest I've found is Loop through all nested dictionary values?

Community
  • 1
  • 1
Tim Clemans
  • 176
  • 1
  • 1
  • 7

1 Answers1

0
def myprint(d, groups):
    new_d = {}
    for k, v in d.items():
        if isinstance(v, dict):
            new_d[k] = myprint(v, groups)
        elif isinstance(v, str):
            new_d[k] = groups[v[1:]] if v.startswith('*') else v
        else:
            new_d[k] = v
    return new_d
Tim Clemans
  • 176
  • 1
  • 1
  • 7