Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配多行python正则表达式_Python_Regex - Fatal编程技术网

匹配多行python正则表达式

匹配多行python正则表达式,python,regex,Python,Regex,我有一个包含配置的文件。我需要找到一个模式来匹配配置文件中的多行 基本上,我正在寻找以下类型的线路: class-map match-any virtualserver1 description virtualserver1.aaa.com 2 match virtual-address 172.16.211.153 tcp eq https 3 match virtual-address 172.16.211.153 tcp eq https class-map

我有一个包含配置的文件。我需要找到一个模式来匹配配置文件中的多行

基本上,我正在寻找以下类型的线路:

class-map match-any virtualserver1
     description virtualserver1.aaa.com
     2 match virtual-address 172.16.211.153 tcp eq https
     3 match virtual-address 172.16.211.153 tcp eq https
class-map match-any virtual-server2
     2 match virtual-address 172.16.211.154 tcp eq http
class-map match-any vip-helloworld
     description vs-yyy.com
class-map match-any vip-myvirtualServer
文件中的块如下所示:

class-map match-any virtualserver1
  description virtualserver1.aaa.com
  2 match virtual-address 172.16.211.153 tcp eq https
  3 match virtual-address 172.16.211.153 tcp eq https
稍后,我需要获取虚拟服务器的名称:virtualserver1 说明(virtualserver1.aaa.com)(如果存在) 以及多个虚拟地址和端口(172.16.211.153和https)(如果存在)

我尝试了各种组合,试图匹配块,但没有成功

import re
fh = open('config_file.txt')
fileData = fh.read()
vipData = re.findall('^class-map match-.*\n.+', fileData,re.MULTILINE)
finalList = sorted(set(vipData))
i = 1
for data in finalList:
    print str(i) +" "+ str(data)
    i = i + 1
这只提供了第一行和第二行作为当前所有配置的输出


我应该使用什么模式来匹配所有块?

re.findall(r'(?好吧,如果块中的“匹配”不能超过2个,您可以尝试使用以下正则表达式:

re.findall(r'(?<=class-map match-any).*?(?=class-map match-any|$)', my_str, re.DOTALL)
class\-map\s+match\-any\s+(?P<servername>[\w-]+)(?:\s*description\s*(?P<description>[\w\.-]+))?(?:\s*\d+\s+match\s*virtual-address\s*(?P<IP>\d+\.\d+\.\d+\.\d+)\s+[^\r\n]*(?P<HTTP1>https?))?(?:\s*\d+\s+match\s*virtual-address\s*(?P<IP2>\d+\.\d+\.\d+\.\d+)\s+[^\r\n]*(?P<HTTP2>https?))?

重新导入
p=re.compile(ur'class \-map\s+match \-any\s+(?p[\w-]+)(?:\s*description\s*(?p[\w\.-]+))(?:\s*\d+\s+match\s*虚拟地址\s*(?p\d+\.\d+\.\d+\d+\d+\s+[^\r\n]*(?Phttps?)(?:\s*\d*\d+\s+match\s+匹配\s*虚拟地址\s*)(?p\d+\d+.\d+.\d+.\d+.\d+.\d+.\r+.\d+.\d+.\d+.\r+.\d+.\d+.\s+.\r+\r+\n+)*?)多行(?Phttps
str=u“你的字符串”
关于findall(p,str)

正则表达式不能一次完成这项工作,因为它不是一种编程语言,没有循环的概念,因此无法多次捕获同一序列。相反,您需要用Python编写循环,并一次捕获一个块。此外,由于要捕获未指定数量的IP地址,您需要我需要第二个正则表达式来提取Python循环中的那些块。@Arkanon可以用一次过程捕获所有块。这些答案对你有用吗?
servername
description
IP
HTTP1
HTTP2
import re
p = re.compile(ur'class\-map\s+match\-any\s+(?P<servername>[\w-]+)(?:\s*description\s*(?P<description>[\w\.-]+))?(?:\s*\d+\s+match\s*virtual-address\s*(?P<IP>\d+\.\d+\.\d+\.\d+)\s+[^\r\n]*(?P<HTTP1>https?))?(?:\s*\d+\s+match\s*virtual-address\s*(?P<IP2>\d+\.\d+\.\d+\.\d+)\s+[^\r\n]*(?P<HTTP2>https?))?', re.MULTILINE | re.DOTALL)
str = u"YOUR_STRING"

re.findall(p, str)