Home > database >  How to match with Regex a newline that before does not have a pattern
How to match with Regex a newline that before does not have a pattern

Time:11-03

There's a way to match from a text with a regex a newline that before does not have a specific patter?

An example could be a newline that not has the end tag </u>

So if there's the string

caaaaa
aaa
<u>refnkrnkfenr</u>
rllllrmlef
rhgfvhr
hbgfdsa

has to match 1st 2nd 4th 5th 6th line

The try with a negative lookahead /(?!\<\/u\>)(?:\r\n|\r|\n)/g does not works and I don't know why.

What's the correct way to have the correct match?

CodePudding user response:

You can use:

(?<!\<\/u\>)(?:\r\n|\r|\n|\Z)

Regex demo.

Note the negative look-behind (?<!...) instead of (?!...). Also I've added \Z to catch end of file.

  • Related