Regular expression to match EOF

The answer to this question is \Z took me awhile to figure it out, but it works now. Note that conversely, \A matches beginning of the whole string (as opposed to ^ and $ matching the beginning of one line).


EOF is not actually a character. If you have a multi-line string, then '$' will match the end of the string as well as the end of a line.

In Perl and its brethren, \A and \Z match the beginning and end of the string, totally ignoring line-breaks.

GNU extensions to POSIX regexes use \` and \' for the same things.


In Visual Studio, you can find EOF like so: $(?![\r\n]). This works whether your line endings are CR, CRLF, or just LF.

As a bonus, you can ensure all your code files have a final newline marker like so:

               Find What: (?<![\r\n])$(?![\r\n])
            Replace With: \r\n
 Use Regular Expressions: checked
Look at these file types: *.cs, *.cshtml, *.js

How this works:

Find any line end (a zero-width match) that is not preceded by CR or LF, and is also not followed by CR or LF. Some thought will show you why this works!

Note that you should Replace With your desired line-ending character, be it CR, LF, or CRLF.