Python 正则表达式匹配特定字符串和结束字符之间的字符串

Python Regex match string between specific string and end character

我正在构建文件剥离器来构建配置报告,并且我有一个非常非常长的字符串作为我的基础数据。以下是它的一个非常小的片段,但它至少说明了我正在使用的东西。

代码段示例:DEFAULT_GATEWAY=192.168.88.1&DELVRY_AGGREGATION_INTERVAL0=1&DELVRY_AGGREGATION_INTERVAL1=1&DELVRY_SCHEDULE0=1&DELVRY_SNI0=192.168.88.158&DELVRY_USE_SSL_TLS1=0&

我将如何匹配以下内容:

between "DEFAULT_GATEWAY=" and "&"
between "DELVRY_AGGREGATION_INTERVAL0=" and "&"
between "DELVRY_AGGREGATION_INTERVAL1=" and "&"
between "DELVRY_SCHEDULE=" and "&"
between "DELVRY_SNI0=" and "&"
between "DELVRY_USE_SSL_TLS1=" and "&"

并用它构建字典:

{"DEFAULT_GATEWAY":"192.168.88.1",
 "DELVRY_AGGREGATION_INTERVAL0":"1",
 "DELVRY_AGGREGATION_INTERVAL1":"1",
 "DELVRY_SCHEDULE0":"1",
 "DELVRY_SNI0":"0",
 "DELVRY_USE_SSL_TLS1":"0"}

?

这是一种方法。

In [1]: input = 'DEFAULT_GATEWAY=192.168.88.1&DELVRY_AGGREGATION_INTERVAL0=1&DELVRY_AGGREGATION_INTERVAL1=1&DELVRY_SCHEDULE0=1&DELVRY_SNI0=192.168.88.158&DELVRY_USE_SSL_TLS1=0&'

In [2]: input.split('&')
Out[2]: 
['DEFAULT_GATEWAY=192.168.88.1',
 'DELVRY_AGGREGATION_INTERVAL0=1',
 'DELVRY_AGGREGATION_INTERVAL1=1',
 'DELVRY_SCHEDULE0=1',
 'DELVRY_SNI0=192.168.88.158',
 'DELVRY_USE_SSL_TLS1=0',
 '']

In [3]: [keyval.split('=') for keyval in input.split('&') if keyval]
Out[3]: 
[['DEFAULT_GATEWAY', '192.168.88.1'],
 ['DELVRY_AGGREGATION_INTERVAL0', '1'],
 ['DELVRY_AGGREGATION_INTERVAL1', '1'],
 ['DELVRY_SCHEDULE0', '1'],
 ['DELVRY_SNI0', '192.168.88.158'],
 ['DELVRY_USE_SSL_TLS1', '0']]

In [4]: dict(keyval.split('=') for keyval in input.split('&') if keyval)
Out[4]: 
{'DEFAULT_GATEWAY': '192.168.88.1',
 'DELVRY_AGGREGATION_INTERVAL0': '1',
 'DELVRY_AGGREGATION_INTERVAL1': '1',
 'DELVRY_SCHEDULE0': '1',
 'DELVRY_SNI0': '192.168.88.158',
 'DELVRY_USE_SSL_TLS1': '0'}

备注

  1. 这是输入线
  2. & 拆分以获得键值对。注意最后一个条目是空的
  3. 用等号拆分每个条目,同时丢弃空条目
  4. 建立字典

另一个解决方案

In [8]: import urlparse

In [9]: urlparse.parse_qsl(input)
Out[9]: 
[('DEFAULT_GATEWAY', '192.168.88.1'),
 ('DELVRY_AGGREGATION_INTERVAL0', '1'),
 ('DELVRY_AGGREGATION_INTERVAL1', '1'),
 ('DELVRY_SCHEDULE0', '1'),
 ('DELVRY_SNI0', '192.168.88.158'),
 ('DELVRY_USE_SSL_TLS1', '0')]

In [10]: dict(urlparse.parse_qsl(input))
Out[10]: 
{'DEFAULT_GATEWAY': '192.168.88.1',
 'DELVRY_AGGREGATION_INTERVAL0': '1',
 'DELVRY_AGGREGATION_INTERVAL1': '1',
 'DELVRY_SCHEDULE0': '1',
 'DELVRY_SNI0': '192.168.88.158',
 'DELVRY_USE_SSL_TLS1': '0'}

首先用'&'分割得到一个字符串列表,然后用'='分割,像这样:

d = dict(kv.split('=') for kv in line.split('&'))
import re

keys = {"DEFAULT_GATEWAY",
    "DELVRY_AGGREGATION_INTERVAL0",
    "DELVRY_AGGREGATION_INTERVAL1",
    "DELVRY_SCHEDULE0",
    "DELVRY_SNI0",
    "DELVRY_USE_SSL_TLS1"}
resdict = {}
for k in keys:
    pat = '{}([^&])&'.format(k)
    mo = re.search(pat, bigstring)
    if mo is None: continue  # no match
    resdict[k] = mo.group(1)
如果 bigstring 是您要搜索的字符串,

将在 resdict 中留下您想要的结果。

这假设您事先知道要查找哪些键,并且将它们保存在一个集合中 keys。如果您事先不知道感兴趣的键,那当然是一个非常不同的问题。