Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex, searching for multiple newlines

I have a regex which searches for a newline symbol:

\r?\n|\r

And two examples:

#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#

Demo: https://regex101.com/r/zQ3nR3/1

I need to update a regex, so it finds only multiple instances of newline symbol, like these in Example-1 between text lines and not the one between text lines in Example-2

like image 229
R-J Avatar asked Nov 02 '25 03:11

R-J


1 Answers

To match just the multiple linebreaks inside #Example text, use a limiting quantifier {2,} applied to (?:\r?\n|\r) and restrict it with a look-ahead:

(\r?\n|\r){2,}(?!#Example-\d)

See demo

The (?!#Example-\d) look-ahead returns a match only if there is no #Example-+digit after the multiple newline symbols.

like image 188
Wiktor Stribiżew Avatar answered Nov 03 '25 17:11

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!