0

The output is not the expected. I would like it to print all diffs regarding 'system' under the 'system' directory and all diffs related to 'interfaces' under the 'interface' directory. "{ ... }" is also not printing even though I have a statement to catch it. The code is below:

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

Output gives:

/templates/juniper/junos/vfirewall/system
[edit system name-server]
+   4.4.4.4;
/templates/juniper/junos/vfirewall/interfaces
>>> 

Expected output:

/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;
+           }
+       }
+   }

1 Answers1

1

Two main problems:

  1. You should use raw strings for the regular expressions, so that the escape sequences will be passed literally to the regexp engine, instead of being processed as string escapes.

  2. You need to use re.search() rather than re.match(), since the latter matches only at the beginning of the string (it's equivalent to starting the RE with ^).

Minor issues: . in ... should be escaped to match literally, and - doesn't need to be escaped.

Working version:

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
Barmar
  • 741,623
  • 53
  • 500
  • 612