python re: 匹配除双换行符以外的所有内容
python re: match everything but a double linebreak
这里有很多这样的问题,但我还没有找到一个是我需要的。
我需要一个可以匹配除双换行符之外的任何内容的正则表达式。更具体地说,这是一个例子:
数据:
# 1 main header
__1.1__ company consents to transfer of the following end user license - including...
__1.1.1__ A subparagraph
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }
我想要做的是找到以 __[\d+\.?]+__
开头并以 \{: #\w+ \}
结尾的每个部分。
为此,我需要正则表达式的中间部分匹配除双换行符以外的任何内容。我一开始是这样做的:__([\d+\.?]+)__.*\{: (#\w+) \}
,但因为它也捕捉到双换行符,所以我得到了从 __1.1__
到 {: #lorem_section }
的所有内容,而我实际上想要从 __1.2__
到 [=] 的所有内容17=].
我读到了负前瞻,这可能是我所需要的,但我似乎无法使其正常工作。我尝试了以下 2 个正则表达式,但都没有产生任何结果
__([\d+\.?]+)__.*(?!\n\n)\{: (#\w+) \}
__([\d+\.?]+)__(?!\n\n)*\{: (#\w+) \}
你需要使用负前瞻。
r'(?s)__[\d.]+__(?:(?!\n\n).)*?\{: #\w+ \}'
(?s)
启用 DOTALL 模式,这使得出现在正则表达式中的点也匹配换行符。 (?:(?!\n\n).)*?
将对任何字符进行非贪婪匹配,但不匹配 \n\n
零次或多次。
>>> s = '''# 1 main header
__1.1__ company consents to transfer of the following end user license - including...
__1.1.1__ A subparagraph
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }'''
>>> m = re.findall(r'(?s)__[\d.]+__(?:(?!\n\n).)*?\{: #\w+ \}', s)
>>> for i in m:
print(i)
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }
或
你也可以这样做。
>>> for i in s.split('\n\n'):
if re.match(r'(?s)__[\d.]+__.*\{: #\w+ \}$', i):
print(i)
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }
与不捕获双行结尾的问题分开,请注意 + 和 ?字符组 [\d+\.?]
中的字符被视为普通字符而不是量词。也就是说,它们会匹配自己,这样您的表达式 __[\d+\.?]+__
就会匹配这样的表达式:
__?__
__+__
__?+???__
等等,如图https://regex101.com/r/sQ8iN1/2
如果您希望应用量词并且希望重复分组,则需要使用圆括号而不是方括号,如 https://regex101.com/r/sQ8iN1/3
这里有很多这样的问题,但我还没有找到一个是我需要的。
我需要一个可以匹配除双换行符之外的任何内容的正则表达式。更具体地说,这是一个例子:
数据:
# 1 main header
__1.1__ company consents to transfer of the following end user license - including...
__1.1.1__ A subparagraph
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }
我想要做的是找到以 __[\d+\.?]+__
开头并以 \{: #\w+ \}
结尾的每个部分。
为此,我需要正则表达式的中间部分匹配除双换行符以外的任何内容。我一开始是这样做的:__([\d+\.?]+)__.*\{: (#\w+) \}
,但因为它也捕捉到双换行符,所以我得到了从 __1.1__
到 {: #lorem_section }
的所有内容,而我实际上想要从 __1.2__
到 [=] 的所有内容17=].
我读到了负前瞻,这可能是我所需要的,但我似乎无法使其正常工作。我尝试了以下 2 个正则表达式,但都没有产生任何结果
__([\d+\.?]+)__.*(?!\n\n)\{: (#\w+) \}
__([\d+\.?]+)__(?!\n\n)*\{: (#\w+) \}
你需要使用负前瞻。
r'(?s)__[\d.]+__(?:(?!\n\n).)*?\{: #\w+ \}'
(?s)
启用 DOTALL 模式,这使得出现在正则表达式中的点也匹配换行符。 (?:(?!\n\n).)*?
将对任何字符进行非贪婪匹配,但不匹配 \n\n
零次或多次。
>>> s = '''# 1 main header
__1.1__ company consents to transfer of the following end user license - including...
__1.1.1__ A subparagraph
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }'''
>>> m = re.findall(r'(?s)__[\d.]+__(?:(?!\n\n).)*?\{: #\w+ \}', s)
>>> for i in m:
print(i)
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }
或
你也可以这样做。
>>> for i in s.split('\n\n'):
if re.match(r'(?s)__[\d.]+__.*\{: #\w+ \}$', i):
print(i)
__1.2__ company also consents to other stuff...
Lorem ipsum dolor sit amet, consectetur aquisquam veniam!
{: #lorem_section }
与不捕获双行结尾的问题分开,请注意 + 和 ?字符组 [\d+\.?]
中的字符被视为普通字符而不是量词。也就是说,它们会匹配自己,这样您的表达式 __[\d+\.?]+__
就会匹配这样的表达式:
__?__
__+__
__?+???__
等等,如图https://regex101.com/r/sQ8iN1/2
如果您希望应用量词并且希望重复分组,则需要使用圆括号而不是方括号,如 https://regex101.com/r/sQ8iN1/3