你需要纠正你的模式。这个
/gm
末尾的部分对应于regex的标志。其他一些事情
'/'
也不需要。
import json
import re
# fixed variable name
response = "{'BB912E94CDE0B0E': (None, 'n_objects=179:n-bytes-memory=0:stop-writes-count=0:set-enable-xdr=use-default:disable-eviction=true:set-delete=false;\n')}"
# fixed pattern
p = ".*'n_objects=([0-9]+):.*"
stringResponse = json.dumps(response)
print stringResponse
print type(response)
# fixed flags parameter (but you do not need it in your example)
print re.match(p,stringResponse, flags=re.M).group(1)
输出:
"{'BB912E94CDE0B0E': (None, 'n_objects=179:n-bytes-memory=0:stop-writes-count=0:set-enable-xdr=use-default:disable-eviction=true:set-delete=false;\n')}"
<type 'str'>
179
使用regex101.com时,还应切换到
python
模式。