Python Regex Sub - Use Match as Dict Key in Substitution -


i'm translating program perl python (3.3). i'm new python. in perl, can crafty regex substitutions, such as:

$string =~ s/<(\w+)>/$params->{$1}/g; 

this search through $string, , each group of word characters enclosed in <>, substitution $params hashref occur, using regex match hash key.

what best (pythonic) way concisely replicate behavior? i've come along these lines:

string = re.sub(r'<(\w+)>', (what here?), string) 

it might nice if pass function maps regex matches dict. possible?

thanks help.

you can pass callable re.sub tell match object.

s = re.sub(r'<(\w+)>', lambda m: replacement_dict.get(m.group()), s) 

use of dict.get allows provide "fallback" if said word isn't in replacement dict, i.e.

lambda m: replacement_dict.get(m.group(), m.group())  # fallback leaving word there if don't have replacement 

i'll note when using re.sub (and family, ie re.split), when specifying stuff exists around wanted substitution, it's cleaner use lookaround expressions stuff around match doesn't subbed out. in case i'd write regex like

r'(?<=<)(\w+)(?=>)' 

otherwise have splicing out/back in of brackets in lambda. clear i'm talking about, example:

s = "<sometag>this stuff<othertag>this other stuff<closetag>"  d = {'othertag': 'blah'}  #this doesn't work because `group` returns whole match, including non-groups re.sub(r'<(\w+)>', lambda m: d.get(m.group(), m.group()), s) out[23]: '<sometag>this stuff<othertag>this other stuff<closetag>'  #this output isn't ideal... re.sub(r'<(\w+)>', lambda m: d.get(m.group(1), m.group(1)), s) out[24]: 'sometagthis stuffblahthis other stuffclosetag'  #this works, ugly , hard maintain re.sub(r'<(\w+)>', lambda m: '<{}>'.format(d.get(m.group(1), m.group(1))), s) out[26]: '<sometag>this stuff<blah>this other stuff<closetag>'  #lookbehind/lookahead makes nicer. re.sub(r'(?<=<)(\w+)(?=>)', lambda m: d.get(m.group(), m.group()), s) out[27]: '<sometag>this stuff<blah>this other stuff<closetag>' 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -