Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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,可以使用正则表达式解析这样一行: name=This is tricky pos=81 key=4565 count=1 pass=a_23 find=2 分组 ('name=This is tricky', 'pos=81'...) 您可以使用将返回以下结果的\w+=拆分文本: >>> re.split(r'(\w+=)',s) ['', 'name=', 'This is tricky ', 'pos=', '81 ', 'key=', '4565 ', 'count=

可以使用正则表达式解析这样一行:

name=This is tricky pos=81 key=4565 count=1 pass=a_23 find=2
分组

('name=This is tricky', 'pos=81'...)

您可以使用将返回以下结果的
\w+=
拆分文本:

>>> re.split(r'(\w+=)',s)
['', 'name=', 'This is tricky ', 'pos=', '81 ', 'key=', '4565 ', 'count=', '1 ', 'pass=', 'a_23 ', 'find=', '2']
然后在列表中使用
zip
,连接相关元素:

>>> sp=re.split(r'(\w+=)',s)[1:]
>>> [''.join(i) for i in zip(sp[0::2],sp[1::2])]
['name=This is tricky ', 'pos=81 ', 'key=4565 ', 'count=1 ', 'pass=a_23 ', 'find=2']