Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式与Python_Python_Regex - Fatal编程技术网

正则表达式与Python

正则表达式与Python,python,regex,Python,Regex,我对正则表达式非常陌生,我想知道如何转换一个表达式,例如 $a * ($aa - $b) 进入 我正在使用Python的re。非常感谢 所以,任何前面有美元符号的字符串都应该用方括号括起来?如果您想知道,请先看一些正则表达式教程,并在这里发布之前尝试这样做。 >>> import re >>> strs = '$a * ($aa - $b)' >>> re.sub(r'\$(\w+)', r'[\1]', strs) '[a] * ([aa

我对正则表达式非常陌生,我想知道如何转换一个表达式,例如

$a * ($aa - $b)
进入


我正在使用Python的re。非常感谢

所以,任何前面有美元符号的字符串都应该用方括号括起来?如果您想知道,请先看一些正则表达式教程,并在这里发布之前尝试这样做。
>>> import re
>>> strs = '$a * ($aa - $b)'
>>> re.sub(r'\$(\w+)', r'[\1]', strs)
'[a] * ([aa] - [b])'
>>> import re
>>> strs = '$a * ($aa - $b)'
>>> re.sub(r'\$(\w+)', r'[\1]', strs)
'[a] * ([aa] - [b])'