我正在编写一个python脚本,并希望匹配一个大文件中的所有Group对象名称,原始数据的示例如下所示:
IT_PC (Group) -Host: 192.168.103.144 -Host: 192.168.103.145 -Network: 192.168.103.0 255.255.255.0 HR_PC (Group) -Host: 192.168.65.145 -Host: 192.168.62.146 -Host: 192.168.62.154
Finance_PC (Group) -Finance_PC_192.168.41.125
Testing_PC (Group) -Host: 192.168.129.1 -Host: 192.168.129.97 -Host: 192.168.59.81 -Host: 192.168.59.82
我要求的输出应该是这样的:
IT_PC (Group)
HR_PC (Group)
Finance_PC (Group)
Testing_PC (Group)
我正在尝试使用下面的常规快递来匹配我所需的结果,但它只返回第一个['IT_PC(组)']。对我有任何建议,谢谢。
source = "IT_PC (Group) -Host: 192.168.103.144 -Host: 192.168.103.145 -Network: 192.168.103.0 255.255.255.0 HR_PC (Group) -Host: 192.168.65.145 -Host: 192.168.62.146 -Host: 192.168.62.154 Finance_PC (Group) -Finance_PC_192.168.41.125 Testing_PC (Group) -Host: 192.168.129.1 -Host: 192.168.129.97 -Host: 192.168.59.81 -Host: 192.168.59.82"
data = ".*? (?= \(group\))"
a = re.findall(data, source)
print a
答案 0 :(得分:1)
我使用的\w+(?= \(Group\))
表达式会匹配一个或多个字母数字字符(A-Za-z0-9_
),后跟空格和(Group)
:
>>> re.findall(r"\w+(?= \(Group\))", source)
['IT_PC', 'HR_PC', 'Finance_PC', 'Testing_PC']
或者,您可以更加具体地了解组对象名称格式,并在下划线后需要一个或多个大写字母:
>>> re.findall(r"\w+_[A-Z]+(?= \(Group\))", source)
['IT_PC', 'HR_PC', 'Finance_PC', 'Testing_PC']
或者,如果您还需要Group
:
>>> re.findall(r"\w+ \(Group\)", source)
['IT_PC (Group)', 'HR_PC (Group)', 'Finance_PC (Group)', 'Testing_PC (Group)']
>>> re.findall(r"\w+_[A-Z]+ \(Group\)", source)
['IT_PC (Group)', 'HR_PC (Group)', 'Finance_PC (Group)', 'Testing_PC (Group)']