看看这个:
set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit
set policy id 1508
exit
set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit
set policy id 1507
set service "TCP-37"
exit
我想在退出Python列表之前设置整行,这意味着:
list=[
'set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit\n\nset policy id 1508',
'set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit\n\nset policy id 1507\n\nset service "TCP-37"'
]
如何使用正则表达式执行此操作?
请注意,我希望在退出其中一个列表索引之前使用整个代码。我的意思是:
list=[
'set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit\n\nset policy id 1508',
'set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit\n\nset policy id 1507\n\nset service "TCP-37"'
]
不是这个:
list=[
'set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit',
'set policy id 1508',
'set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit',
'set policy id 1507',
'set service "TCP-37"'
]
我想要
re.findall()
用于查找由set启动并以exit结束的字符串的模式。
罐中。
答案 0 :(得分:0)
我们认为文本位于一个名为text的变量中。您可以使用str类的split方法(https://docs.python.org/2/library/stdtypes.html#str.split)。 在Python解释器中:
>>> t = """set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit
...
... set policy id 1508
...
... exit
...
... set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit
...
... set policy id 1507
...
... set service "TCP-37"
...
... exit"""
>>> t.split("exit")
['set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit\n\nset policy id 1508\n\n', '\n\nset policy id 1507 from "C
MS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit\n\nset policy id 1507\n\nset service "TCP-37"\n\n', '']
如果要避免列表中的最后一个空元素,可以使用" exit \ n"来调用split方法。 您还可以在列表的每个项目上调用strip方法,以删除每个元素开头和结尾的空格(这将为您提供预期的输出)。
答案 1 :(得分:0)
s="""
set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit
set policy id 1508
exit
set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit
set policy id 1507
set service "TCP-37"
exit
"""
import re
re.findall('(set.*?)^exit',s,re.DOTALL|re.MULTILINE)
['set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit\n\nset policy id 1508\n\n', 'set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit\n\nset policy id 1507\n\nset service "TCP-37"\n\n']
如果你想要所有这些都是单行:
In [2]: import re
In [3]: lines = re.findall('(set.*?)^exit',s, re.DOTALL | re.MULTILINE)
In [4]: joined = [" ".join(x.split()) for x in lines]
In [5]: joined[0]
Out[5]: 'set policy id 1508 from "TEST" to "OPR" exit "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit set policy id 1508'
In [6]: joined[1]
Out[6]: 'set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit set policy id 1507 set service "TCP-37"'
答案 2 :(得分:0)
您可以尝试使用以下代码,该代码使用负前瞻和(DOTALL修饰符),
>>> import re
>>> s = """set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit
...
... set policy id 1508
...
... exit
...
... set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit
...
... set policy id 1507
...
... set service "TCP-37"
...
... exit"""
>>> m = re.findall(r'set(?:(?!exit).)*', s, re.DOTALL)
>>> m
['set policy id 1508 from "TEST" to "OPR" "10.2.197.3/32" "ARG-VCENTER-SRV-1" "ANY" permit\n\nset policy id 1508\n\n', 'set policy id 1507 from "CMS-SrvFarm" to "DMZ" "CMS-SRV5(10.2.90.14)" "THR-CMS-SRV-0" "TCP-123" permit\n\nset policy id 1507\n\nset service "TCP-37"\n\n']