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:
- 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! - 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.