如何匹配从一个字符开始并以相同字符结束的模式,但不包括匹配中的最后一个字符?

2024-09-29 23:19:02 发布

您现在位置:Python中文网/ 问答频道 /正文

如果你觉得标题有点晦涩,我的意思是:我在寻找每一个以哈希符号(#)开头的模式,然后匹配该符号之后的所有内容,直到找到另一个哈希或另一个定义的条目,但最后一个哈希或其他条目不应该是匹配的一部分

举个例子:

#one_liner = some cr4zy && weird stuff h3r3 $% ()
#multi_liner =some other s7uff,
    but put in (other) line
#one_liner_again = again, some stuff here...
LB: this line shouldn't be taken into consideration!
#multi_liner_again=You guessed:
going to another line!
<EOF>

我想以四个匹配结束,例如包含这样一组元组:

("one_liner", "some cr4zy && weird stuff h3r3 \$\% ()")
("multi_liner", "some other s7uff, but put in (other) line")
("one_liner_again", "again, some stuff here...")
("multi_liner_again", "You guessed: going to another line!")

我尝试过这种模式,但它没有带来我想要的:

#\w.+\s*=(\s*|\S*.+)\w.+\n*\s*.+(?=#)


Tags: line模式符号条目someonemultiother
1条回答
网友
1楼 · 发布于 2024-09-29 23:19:02

这可能会让您开始…您可能会希望除去找到的\n值和不需要的空格

import re

data = """
#one_liner = some cr4zy && weird stuff h3r3 $% ()
#multi_liner =some other s7uff,
    but put in (other) line
#one_liner_again = again, some stuff here...
   LB: this line shouldn't be taken into consideration!
#multi_liner_again=You guessed: 
going to another line!
"""

for m in re.findall(r"#([^#]+)\s*=\s*((?:[^#](?!LB:))*)", data, re.MULTILINE|re.DOTALL):
    print(m)

印刷品:

('one_liner ', 'some cr4zy && weird stuff h3r3 $% ()\n')
('multi_liner ', 'some other s7uff,\n    but put in (other) line\n')
('one_liner_again ', 'again, some stuff here...\n  ')
('multi_liner_again', 'You guessed: \ngoing to another line!\n')

相关问题 更多 >

    热门问题