python代码中side []中的正则表达式

时间:2013-09-23 13:44:38

标签: python regex

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

我想阅读:[enable |禁用]

感谢

1 个答案:

答案 0 :(得分:0)

为正则表达式添加\ [。* \]

检查验证链接

http://regex101.com/r/lL9tQ7

JJ