gpt4 book ai didi

python - 匹配键值对

转载 作者:行者123 更新时间:2023-11-30 22:17:26 25 4
gpt4 key购买 nike

在下面的文本中,我想提取键及其值。我编写了以下正则表达式,但它与多行中的值不匹配。正则表达式:--(.*)=.*(?=(.|--|\n|\Z)*)

--some text here not to be matched
--key1=this is a
multiline statement
statement
--random text not to be matched
--key2=val2
--key3=val3
--random text here not to be matched

所以,匹配后输出应该是

--key1=this is a
multiline statement
statement
--key2=val2
--key3=val3

最佳答案

你可以试试这个:

import re
s = """
--some text here not to be matched
--key1=this is a
multiline statement
statement
--random text not to be matched
--key2=val2
--key3=val3
--random text here not to be matched
"""
new_data = re.findall('\-\-\w+\=[a-zA-Z\s\n]+', s)
for i in new_data:
print(i)

输出:

--key1=this is a
multiline statement
statement
--key2=val
--key3=val

关于python - 匹配键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49678510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com