使用正则表达式在数组中查找特定匹配项

2024-09-28 21:38:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在论坛中搜索了ben,但是没有运气去做我想在数组中搜索正则表达式的事情。我尝试过findallmatch,并在re库上进行筛选,但我无法实现我想要的,但使用fnmatch函数我做到了,但我知道fnmatch不像re那样强大。你知道吗

以下是阵列的外观:

[0] rule 5 permit sctp source 14.191.156.0 0.0.0.15 destination 201.175.131.64 0.0.0.7
[1] rule 60 permit icmp source 14.191.210.34 0 destination 201.175.137.78 0
[2] rule 61 permit tcp source 14.191.210.34 0 destination 201.175.137.78 0
[3] rule 5000 deny ip

基本上,我要筛选与此正则表达式匹配的规则:

rule* icmp|sctp|udp|tcp* and that end is "deny ip"

1。你知道吗

pattern = re.compile('rule* icmp|sctp|udp|tcp*')
if pattern.search(array)

我可以用fnmatch来做,但是我必须做四次,因为它不支持or reg表达式(|

if fnmatch(acl[j],'*rule * permit icmp*'):

2。你知道吗

另外,我想过滤,如果它以拒绝ip结束做另一个动作。你知道吗

pattern = re.compile('deny ip$')

Tags: ipresourceifruledestinationtcpsctp