正则表达式模式,用于过滤REST API的多行数据

将ASA从ASDM迁移到FMC,包括访问策略。完成项目的步骤之一是将网络/服务对象及其组迁移到FMC。计划通过使用正则表达式过滤ASA对象(从ASA配置中)并在REST API上运行python脚本来创建对象。现在,我目前遇到的问题是,有大量数据要移交给FMC,超过3000行。

当前尝试提供一个正则表达式模式,该模式将过滤多行字符串并匹配REST API的数据。使用regex101执行此任务。使用当前的正则表达式模式,我仅匹配前两行的数据。我遇到的另一个问题之一是,并非所有行都包含“ destination eq”,此后正则表达式与“ port_no”匹配。

有人可以协助进行正则表达式表达吗?根据当前的正则表达式表达式,要匹配“对象组服务”,“服务对象”和“目的地等价”之后的数据,或者匹配不存在“目的地等价”时的数据?

谢谢

正则表达式:

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

要过滤的数据:

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
zdp888 回答:正则表达式模式,用于过滤REST API的多行数据

如果您要匹配“对象组服务”,“服务对象”和“目的地等式”之后的数据,则可以使用alternation来匹配object-group service或{{1 }}和service-object的可选非捕获组。

destination eq

部分

  • ^\s*(?:object-group service|service-object) (.+?)(?: destination eq (\w+))?$ 字符串的开头
  • ^匹配0+个空格字符
  • \s*匹配1个选项
  • (?:object-group service|service-object)匹配空间并在第1组中捕获,匹配任何非char贪婪
  • (.+?)非捕获组
    • (?:匹配空间和目标eq并在第2组
    • 中捕获1个以上的字符字符
  • destination eq (\w+)关闭组并将其设置为可选
  • )?字符串结尾

Regex demo

本文链接:https://www.f2er.com/3167266.html

大家都在问