Green Cell
Green Cell

Reputation: 4777

Regex findall and replace with dictionary

I have a 2 part question for this. Ultimately I'm trying to search for any string that is contained in $"" with regex then replace it with a value from a dictionary. Here's what I have:

import re

# Dictionary to use to replace with later
myDict = {'hand_R_item':'hand_R_node', 'hand_L_item':'hand_L_node', 'hips_item':'hips_node', 'body_item':'body_node'}

# String to process
command = '''objs = [$"hand_R_item", $"hand_L_item", $"hips_item"]
for obj in objs:
    cmds.select(add = True)
    cmds.parent(obj, $"body_item")
'''

# Find all instances of anything matching $""
regex = re.compile(r'\$\"[\w]*?\"')
allMatches = re.findall(regex, command)

# Replace matches with values from dict
newCommand = command
for match in allMatches:
    newCommand = newCommand.replace(match, myDict.get(match[2:-1], '') )
print newCommand

This will output the following, which is what want:

'objs = [hand_R_node, hand_L_node, hips_node]
for obj in objs:
    cmds.select(add = True)
    cmds.parent(obj, body_node)'

My questions are mainly to see if I'm approaching this the right way:

  1. Is r'\$\"[\w]*?\"' the best pattern to use? I'm not as comfortable with regular expressions so I don't know if I'm missing any pitfalls!
  2. Is there a more efficient way to replace everything instead of looping through the regex results? I feel like there might be a more elegant approach.

Upvotes: 2

Views: 1045

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213223

You can use re.sub straightaway here. Also, in your regex, you don't need to escape the quotes. And \w can be outside the character class:

>>> d = {'hand_R_item':'hand_R_node', 'hand_L_item':'hand_L_node', 'hips_item':'hips_node', 'body_item':'body_node'}
>>> reg = re.compile(r'\$"(\w*?)"')
>>> command = '''objs = [$"hand_R_item", $"hand_L_item", $"hips_item"]
... for obj in objs:
...     cmds.select(add = True)
...     cmds.parent(obj, $"body_item")
... '''
>>> 
>>> # Replace the group 1, with corresponding value from dict
>>> reg.sub(lambda m: d[m.group(1)], command)
'objs = [hand_R_node, hand_L_node, hips_node]\nfor obj in objs:\n    cmds.select(add = True)\n    cmds.parent(obj, body_node)\n'
>>> 

Upvotes: 1

Related Questions