繁体   English   中英

如何匹配包含特定模式的段落与正则表达式?

[英]How to match paragraphs containing a specific pattern with regex?

我有以下段落:

This is paragraph #1
New-York, London, Paris, Berlin
Some other text
End of paragraph

This is paragraph #2
London, Paris
End of paragraph

This is paragraph #3
New-York, Paris, Berlin
Some other text
End of paragraph

This is paragraph #4
End of paragraph

This is paragraph #5
Paris, Berlin
Some other text
End of paragraph

我如何使用正则表达式匹配包含例如纽约(#1和#3)或伦敦(#1,#2)的段落? 甚至是纽约和柏林(#1,#3)?

我在SO中找到了答案

如何匹配使用正则表达式的段落

这允许我匹配段落(两个空白行之间的所有文本)。

但我无法想象(我的正则表达式技能是......有限的)如何匹配包含特定模式的段落,只有那些段落。

在此先感谢您的帮助

注意:我的想法是使用编辑IOS应用程序中的答案来折叠不包含模式的答案。

如果您计划在编辑iOS应用程序中使用该模式,我发现您可能无法访问Python代码本身。

然后,我所能提出的就是模式

(?m)^(?=.*(?:\r?\n(?!\r?\n).*)*?\bNew-York\b)(?=.*(?:\r?\n(?!\r?\n).*)*?\bBerlin\b).*(?:\r?\n(?!\r?\n).*)*

请参阅正则表达式演示 基本上,我们只匹配从行的开头( ^(?m)修饰符),我们检查是否有New-YorkBerlin作为整个单词(由于\\b字边界)在第一行之前的任何地方双线中断,如果存在,则匹配这些线。

细节

  • (?m)^ - 开始行
  • (?=.*(?:\\r?\\n(?!\\r?\\n).*)*?\\bNew-York\\b) - 一个积极的前瞻,确保New-York任何地方都有一个完整的单词在除了换行符之外的0+个字符( .* )之后,可选地跟随0 +连续的CRLF / LF换行符序列,而不是另一个CRLF / LF换行符,其次是换行符
  • (?=.*(?:\\r?\\n(?!\\r?\\n).*)*?\\bBerlin\\b) - 除了换行符之外的0+字符之后的任何地方Berlin的完整字词.* )任选地跟随0 +连续的CRLF / LF换行序列,然后没有跟随另一个CRLF / LF换行,接着是其余的生产线
  • .* - 匹配线
  • (?:\\r?\\n(?!\\r?\\n).*)* - 匹配连续0次以上:
    • \\r?\\n(?!\\r?\\n) - 换行符(CRLF或LF)未跟随另一个CRLF或LF
    • .* - 其余部分。

使用支持空分割的较新的regex模块

import regex as re

string = """
This is paragraph #1
New-York, London, Paris, Berlin
Some other text
End of paragraph

This is paragraph #2
London, Paris
End of paragraph

This is paragraph #3
New-York, Paris, Berlin
Some other text
End of paragraph

This is paragraph #4
End of paragraph

This is paragraph #5
Paris, Berlin
Some other text
End of paragraph
"""

rx = re.compile(r'^$', flags = re.MULTILINE | re.VERSION1)

needle = 'New-York'

interesting = [part 
    for part in rx.split(string)
    if needle in part]

print(interesting)
# ['\nThis is paragraph #1\nNew-York, London, Paris, Berlin\nSome other text\nEnd of paragraph\n', '\nThis is paragraph #3\nNew-York, Paris, Berlin\nSome other text\nEnd of paragraph\n']

我认为你的具体案例根本不需要正则表达式:

[i for i,p in enumerate(mystr.split('\n\n')) if 'New-York' in p or 'London' in p]

在您的情况下导致:

[0, 1, 2]

显然and条件and条件同样容易,或者否定if 仅当您需要段落索引时才使用enumerate 如果你想要段落本身,你不需要它。 无论如何,无需强制使用regex

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM