Python 不应使用正则表达式输出打印列表

Python 不应使用正则表达式输出打印列表,python,printing,logic,conditional-statements,Python,Printing,Logic,Conditional Statements,输出不是预期的。我希望它在“系统”目录下打印与“系统”相关的所有差异,并在“接口”目录下打印与“接口”相关的所有差异。“{…}”也没有打印,即使我有一个语句来捕捉它。代码如下: import re template_list = ['system','interfaces'] directory = '/templates/juniper/junos/vfirewall/' diff = """[edit system name-server] 8.8.8.8 { ... } + 4

输出不是预期的。我希望它在“系统”目录下打印与“系统”相关的所有差异,并在“接口”目录下打印与“接口”相关的所有差异。“{…}”也没有打印,即使我有一个语句来捕捉它。代码如下:

import re
template_list = ['system','interfaces']
directory = '/templates/juniper/junos/vfirewall/'
diff = """[edit system name-server]
    8.8.8.8 { ... }
+   4.4.4.4;
[edit interfaces ge-0/0/0 unit 0 family inet]
+       address 10.20.30.10/24;
-       address 10.50.30.10/24;
[edit interfaces]
+   ge-0/0/1 {
+       unit 2 {
+           family inet {
+               address 10.50.80.10/24;
+           }
+       }
+   }""".splitlines()

for template in template_list:
 print("{}{}".format(directory,template))
 for line in diff:
   if(re.match('\[edit\s({})'.format(template),line)):
     print('{}'.format(line))
   elif(re.match('\{ ... \}',line)):
     print('{}'.format(line))
   elif(re.match('^\-',line)):
     print('{}'.format(line))
   elif(re.match('^\+',line)):
     print('{}'.format(line))
   elif(re.match('\[edit\s\w.+',line)):
     break
输出结果如下:

/templates/juniper/junos/vfirewall/system
[edit system name-server]
+   4.4.4.4;
/templates/juniper/junos/vfirewall/interfaces
>>> 
预期产出:

/templates/juniper/junos/vfirewall/system
[edit system name-server]
    8.8.8.8 { ... }
+   4.4.4.4;
/templates/juniper/junos/vfirewall/interfaces
[edit interfaces ge-0/0/0 unit 0 family inet]
+       address 10.20.30.10/24;
-       address 10.50.30.10/24;
[edit interfaces]
+   ge-0/0/1 {
+       unit 2 {
+           family inet {
+               address 10.50.80.10/24;
+           }
+       }
+   }
两个主要问题:

  • 您应该为正则表达式使用原始字符串,这样转义序列将被逐字传递给regexp引擎,而不是作为字符串转义处理

  • 您需要使用
    re.search()
    而不是
    re.match()
    ,因为后者只在字符串的开头匹配(这相当于以
    ^
    开始re)

  • 次要问题:
    中的
    应该进行转义,以与字面上的匹配,
    -
    不需要进行转义

    工作版本:

    import re
    template_list = ['system','interfaces']
    directory = '/templates/juniper/junos/vfirewall/'
    diff = """[edit system name-server]
        8.8.8.8 { ... }
    +   4.4.4.4;
    [edit interfaces ge-0/0/0 unit 0 family inet]
    +       address 10.20.30.10/24;
    -       address 10.50.30.10/24;
    [edit interfaces]
    +   ge-0/0/1 {
    +       unit 2 {
    +           family inet {
    +               address 10.50.80.10/24;
    +           }
    +       }
    +   }""".splitlines()
    
    for template in template_list:
     print("{}{}".format(directory,template))
     for line in diff:
       if(re.search(r'\[edit\s({})'.format(template),line)):
         print('{}'.format(line))
       elif(re.search(r'\{ \.\.\. \}',line)):
         print('{}'.format(line))
       elif(re.search(r'^-',line)):
         print('{}'.format(line))
       elif(re.search(r'^\+',line)):
         print('{}'.format(line))
       elif(re.search(r'\[edit\s\w.+',line)):
         break
    

    创建正则表达式时使用原始字符串。当然可以,但在包含原始字符串时仍然无法解决我的问题。您还需要使用
    re.search
    而不是
    re.match