正则表达式:搜索多个换行符

3

我有一个正则表达式,用于搜索换行符:

\r?\n|\r

以下是两个例子:

#Example-1#
The correct answer is (B) 2 to 3. jfbgfdgdf sgdsgsd.

jfbgfdgdf sgdsgsd.
#End Example-1#

#Example-2#
The correct answer is (B) 2 to 3. jfbgfdgdf sgdsgsd.
jfbgfdgdf sgdsgsd.
#End Example-2#

演示:https://regex101.com/r/zQ3nR3/1

我需要更新一个正则表达式,使其仅找到多个换行符的实例,比如在Example-1中文本行之间出现的换行符,而不是在Example-2中文本行之间出现的那个换行符。


2
喜欢 (\r?\n|\r){2,} 吗? - Wiktor Stribiżew
你可以像这样使用花括号 \\s{2,}。它只会查找两个或更多个空格的实例。 - SomeJavaGuy
2个回答

3
为了匹配#Example文本内的多个换行符,使用限定符{2,}应用于(?:\r?\n|\r)并通过前瞻进行限制。
(\r?\n|\r){2,}(?!#Example-\d)

请查看演示

(?!#Example-\d)向前查找只有在多个换行符后没有#Example-+数字时才返回匹配项。


2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接