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.
我正在编写一些python正则表达式作为脚本的一部分,我正在编写这个脚本以生成简单的shell脚本。我必须遗漏一些非常简单的东西。
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.
如何匹配并返回字符串中的所有$(*)值?我尝试了一些类似于'$ \((*)\)'的不同正则表达式,但我没有在python正则表达式测试器中获得任何匹配。非常感谢帮助。
1 个解决方案
#1
0
Use the following approach:
使用以下方法:
s = "$(VAR1)/cruel/$(VAR3)/world"
result = re.findall(r'\$\([^()]+\)', s)
print(result)
The output:
['$(VAR1)', '$(VAR3)']
#1
0
Use the following approach:
使用以下方法:
s = "$(VAR1)/cruel/$(VAR3)/world"
result = re.findall(r'\$\([^()]+\)', s)
print(result)
The output:
['$(VAR1)', '$(VAR3)']