Given a string, I need to identify the field after a $
that may or may not be surrounded by { }
:
$verb = verb
${verb}age = verb
$$
acts as an escape and I need to account for that as well as it may precede the delimiting $
.
What I have so far is:
reg = r'\$([_a-zA-Z0-9]*)'
s = '$who likes $what'
re.findall(reg, s)
['who', 'what']
But I cannot devise the expression for the optional bracing, I tried:
reg = r'\$({?[_a-zA-Z0-9]*}?)'
But that picks up values such as:
${who
$who}
What would be the appropriate expression to be able to account for the optional bracing?
Update:
When it comes to preceding $
, the following would be invalid strings:
$$verb = invalid
$${verb} = invalid
But these would be valid:
$$$verb = $verb
$$${verb} = $verb
This is because a $$
is replaced with a single $
afterwards.