嗨,我一直在努力在代码中的 [] 中获取正则表达式,但一切正常,这是 python 脚本。
这是我的代码:
import re
with open("nfile.cli") as f:
newlist = f.read()
cmd = re.search(r'^\W(command=)(".*?")', newlist )
if(cmd == None):
print cmd
else :
newstr = cmd.group()
print newstr
argument = re.search('<.*>', newstr )
print argument.group()
option = re.findall("{.*}", newstr )
print option
kword = re.search('[.*?]', newstr )
print kword.group()
输出:
%command="[enable | disable] port <port-no> {force}"
<port-no>
['{force}']
Traceback (most recent call last):
File "read_command.py", line 16, in <module>
print kword.group()
AttributeError: 'NoneType' object has no attribute 'group'
nfile:
%command="[enable | disable] port <port-no> {force}"%
我想这样读:[启用 | 禁用]
谢谢