Regex Pattern for filtering multiline data for REST API

883 Views Asked by At

Migrating ASAs from ASDM to FMC including access policy. One of the steps to complete the project is to migrate network/service objects, their groups to FMC. Planning to create objects by filtering ASA objects (from ASA configuration) with regex and running python script on REST API. Now the issue which I currently have is a lot of data to move across to FMC, over 3000 lines.

Currently trying to come up with a regex pattern that would filter multiline strings and match data for REST API. Using regex101 for this task. With current regex pattern I am matching data for first two lines only. One of the other problems I came across was that not all lines include 'destination eq' after which regex is matching 'port_no'.

Would someone be able to assist with regex expression? As per current regex expression want to match data that comes after 'object-group service', 'service-object' and 'destination eq' OR when 'destination eq' is not present?

Thanks

Regex expression:

object-group service (?P<name>.+)(?:\n |.)service-object (?P<protocol>.+) destination eq (?P<port_no>\d{0,5} |\w{0,10}.+)\n

Data to filter:

object-group service DM_INLINE_SERVICE_8
 service-object tcp destination eq ldap
 service-object udp destination eq syslog
 service-object object kerberos5-tcp
 service-object object kerberos5-udp
 service-object object ldap-udp
 service-object udp destination eq domain
 service-object object ldap-gcs
 service-object object TCP_3268
 service-object object TCP_3269
 service-object object TCP_445
 service-object tcp-udp destination eq domain
 service-object tcp destination eq ldaps
 service-object udp destination eq ntp
 service-object object TCP_464
object-group network DM_INLINE_NETWORK_13
 network-object object IN_V030_197_memcache_01
 network-object object IN_V030_198_memcache_02
1

There are 1 best solutions below

4
On

If you want to match data that comes after 'object-group service', 'service-object' and 'destination eq' you might use an alternation to match either object-group service or service-object and an optional non capturing group for destination eq.

^\s*(?:object-group service|service-object) (.+?)(?: destination eq (\w+))?$

In parts

  • ^ Start of string
  • \s* Match 0+ whitespace chars
  • (?:object-group service|service-object) Match 1 of the options
  • (.+?) Match space and capture in group 1 matching any char non greedy
  • (?: Non capturing group
    • destination eq (\w+) Match space and destination eq and capture 1+ word chars in group 2
  • )? Close group and make it optional
  • $ End of string

Regex demo