I am using a dictionary that contain regular expressions to substitute portions of different strings, as elegantly described in a previous SO question by @roippi. The first 're.sub' expression works perfectly. However, whenever my code actually involves regex expressions (the second 're.sub' expression), the substitutions don't work.
I am very confused as to why this is the case. I have tried both using and taking out the 'r' as well as incorporating the lookahead/lookbehind expressions, nothing seems to work. Any help would be greatly appreciated!
test_dict = {r'(\d+)': 'THIS IS A NUMBER', 'john_doe':'THIS IS A NAME'}
re.sub('(john_doe)', lambda x: test_dict.get(x.group(1),x.group(1)),'john_doe_jr')
re.sub(r'(\d+)', lambda x: test_dict.get(x.group(1), x.group(1)), '999la')
<(\d+)>pattern does not match anything in999la999, which becomesx.group(1).999is not a key intest_dict, which causes the.getmethod to return999, replacing999with999.