Reputation: 2027
I have been racking my brain with a particular regex problem I currently have.
I want to match values that that could span multiple lines. If the data spans multiple lines it will end with as space than an underscore " _" but there may be valid text in the line that will have a space and underscore before it "This _is".
See the below text sample:
This is d_ata 1
_This is _
data 2
This _is data 3a
This _
is _
data 4
Results would be the following
Match 1
This is d_ata 1
Match 2
_This is _
data 2
Match 3
This _is data 3a
Match 4
This _
is _
data 4
I don't care about the content matching, just ensuring I get the correct end of line matches.
Edit: See Robby's negative look behind solution below.
Had tried it with some additional logic earlier which turned out to be to complicated for my regex provider to handle, simplified it and it worked a treat.
Upvotes: 2
Views: 2422
Reputation: 97302
This PCRE expression should deliver the required result:
/^.*?(?<! _)$/gms
This is using the negative lookbehind (?<! _)
in combination with the multiline flag (m
) to match up to a line end that is not preceded by _
. The single-line flag (s
) ensures that the dot also matches newlines.
Here's a regex101 example.
Upvotes: 2