Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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,如果你觉得标题有点晦涩,我的意思是:我在寻找以散列符号(#)开头的每个模式,然后匹配该符号之后的所有内容,直到找到另一个散列或另一个定义的条目,但最后一个散列或其他条目不应该是匹配的一部分 举个例子: #one_liner = some cr4zy && weird stuff h3r3 $% () #multi_liner =some other s7uff, but put in (other) line #one_liner_again = again, some

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

举个例子:

#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>
我尝试过这种模式,但它没有带来我想要的:


\w.+\s*=(\s*\s*+)\w.+\n*\s*+(?=#))
这可能会让您开始…您可能会想要去掉找到的
\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')

这可能会让您开始…您可能会希望除去找到的
\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')

你所说的定义条目是什么意思?只有以LB开头的sth:?@anubhava:在末尾添加了模式。@mrxra:是的,当找到
LB
时,应该停止匹配。与查找符号类似。定义的条目是什么意思?只有以LB开头的sth:?@anubhava:在末尾添加了模式。@mrxra:是的,当找到
LB
时,应该停止匹配。类似于查找
#
符号。