正则表达式在第一场比赛后停止我需要它继续

2024-04-19 20:22:18 发布

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

我有这个正则表达式:

source_IP = re.findall(r':name\s+[\(]Anti-Spoof-firewallX-eth2-02[\)]\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*:Name\s+[\(](.*)+[\)]'

与以下文本冲突:

^{pr2}$

我需要所有的IP地址,但是regex只返回第一个地址(n-10.10.24.0s22)。如何更正正则表达式?在


Tags: name文本ipresource地址regexspoof
1条回答
网友
1楼 · 发布于 2024-04-19 20:22:18

您需要使用一个更简单的regex,比如:Name\s+\(([^\)]+)\)。在

说明:

:Name查找Name:

\s+后跟一个或多个空格

\(后接(

([^\)]+)捕获()中的所有内容;如果括号可能为空,请将+更改为*

\))结尾

findall的文档说明:

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

所以你的正则表达式的第一部分是不必要的和限制性的。在

import re

source_IP = re.findall(r':Name\s+\(([^\)]+)\)',':name (Anti-Spoof-firewallX-eth2-02) ) :ip_convention_object () :color (black) :comments () :group_convention_query () :group_sort_type (3) :is_convention_on (false) :member_class (network_object) :members_query () :type (group) : (ReferenceObject :Table (network_objects) :Name (n-10.10.24.0_s22) :Uid ("{DACDEBF9-C041-4C48-928E-92ECCC606CD5}") ) : (ReferenceObject :Table (network_objects) :Name (n-10.1.90.0_s23) :Uid ("{3E947D4C-677-45B4-AFCC-553351A7F03E}") ) : (ReferenceObject :Table (network_objects) :Name (n-100.141.28.0_s23) :Uid ("{3EE3901E-7490-4FDF-A66C-C452D17794}") ) : (ReferenceObject :Table (network_objects) :Name (n-10.20.20.0_s22) :Uid ("{51EED8DA-BFDB-42CD-8C3B-71DRRR6111E}") ) : (ReferenceObject :Table (network_objects) :Name (n-10.19.32.0_s23) :Uid ("{58CDB-6FDC-4F42-8F6B-3933428EB408}") ) : (ReferenceObject :Table (network_objects) :Name (n-10.11.16.64_s27) :Uid ("{1D9A7204-ADE5-4414-B264-EA51A8TRE87}") ) )')

print(source_IP)
# outputs: ['n-10.10.24.0_s22', 'n-10.1.90.0_s23', 'n-100.141.28.0_s23', 'n-10.20.20.0_s22', 'n-10.19.32.0_s23', 'n-10.11.16.64_s27']

regex101

相关问题 更多 >