The $ anchor corresponds to a $ matching behavior similar to Perl at the end of a bite or before the last \n in a string. You can still use $ , but limit the end of the line to a negative lookup.
[^\n]$(?!\n)
Watch the regex demo
Since $ can match at the end of a line, but also before the last LF, looking at (?!\n) ensures that it does not match if LF is the last character in the line.
source share