Reputation: 3027
I'm having trouble with some python regex as part of a script that I'm writing to generate simple shell scripts. I must be missing something really simple.
Example strings:
"$(VAR1)/mypath/to/nowhere"
"$(VAR2)"
"/cruel/$(VAR3)/world"
How can I match and return all $(*)
values from the strings? I've tried a bunch of different regexes similar to '$\((*)\)'
, but I'm not getting any matches in a python regex tester. Help is much appreciated.
Upvotes: 1
Views: 1849
Reputation: 92854
Use the following approach:
s = "$(VAR1)/cruel/$(VAR3)/world"
result = re.findall(r'\$\([^()]+\)', s)
print(result)
The output:
['$(VAR1)', '$(VAR3)']
Upvotes: 1