-2

嗨,我一直在努力在代码中的 [] 中获取正则表达式,但一切正常,这是 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}"%

我想这样读:[启用 | 禁用]

谢谢

4

1 回答 1

0

将 \[.*\] 用于正则表达式

检查链接以进行验证

http://regex101.com/r/lL9tQ7

JJ

于 2013-09-23T13:52:27.713 回答