Michal Kordas
Michal Kordas

Reputation: 10925

Regex to find files without newline at the end in IntellliJ

I'm trying to find all files without line feed character at the end of file using Find in Files menu in IntelliJ IDEA.

I've tried regex [^\n]\Z, but it also finds files with newlines.

What is proper regexp to do that? Or maybe there's other way to accomplish this?

Upvotes: 13

Views: 3457

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626926

The $ anchor conforms to the $ Perl-like behavior matching at the end of the sting or before the last \n in the string. You can still use the $, but restrict the end of the string with a negative lookahead.

[^\n]$(?!\n)

See the regex demo

Since $ may match at the end of the string, but also before the last LF, the (?!\n) lookahead makes sure to fail the match if an LF is the last character in the string.

Upvotes: 2

Tamas Rev
Tamas Rev

Reputation: 7166

Try small z: [^\n]\z

More on this here.

Upvotes: 22

Related Questions