【发布时间】:2018-05-04 22:40:25
【问题描述】:
我有以下段落:
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)?
我在 S.O. 中找到了答案。
How match a paragraph using regex
这允许我匹配段落(两个空行之间的所有文本)。
但我不知道(我的正则表达式技能……有限)如何匹配包含特定模式的段落,并且只匹配那些段落。
提前感谢您的帮助
注意:这个想法是使用编辑 IOS 应用程序中的答案来折叠不包含模式的答案。
【问题讨论】:
-
您使用哪种编程语言?首先拆分段落(在空行上)然后在其中查找
New-York可能更容易。 -
哪种风格的正则表达式? Python?你必须在一行中使用正则表达式吗?您链接到的答案拆分为“\n\n”。
-
@kabanus : Python
-
@Jan:我不想拆分段落:我想保留包含指定模式的整个段落,并且只保留那些段落。