使用正则表达式python提取子字符串

Jac*_*ack 0 python regex

我知道这是一个非常常见的问题,但它让我发疯。

我想使用正则表达式来匹配我的字符串中的子字符串。

 line = '##ParameterValue[part I care about]=garbagegarbage'
Run Code Online (Sandbox Code Playgroud)

我想提取part I care about. 我的代码如下所示:

import re
line = '##ParameterValue[part I care about]=garbagegarbage'
m = re.match('\[(.*)\]', line)
print m.group(1)
Run Code Online (Sandbox Code Playgroud)

但这给了我一个 AttributeError: 'NoneType' object has no attribute 'group'

我在regex101上测试了我的正则表达式并且它有效。我不明白为什么这对我来说失败了。

dep*_*erm 7

更改matchsearch

import re
line = '##ParameterValue[part I care about]=garbagegarbage'
m = re.search('\[(.*)\]', line)
print m.group(1)
Run Code Online (Sandbox Code Playgroud)