A full example:
\A(.+)$(?s:.*)^(.+)\Z will match the first and the last line in one go
a simple replacement of
will strip everything except first and last lines.
\A(.+)$\n((?s).*)\n^(.+)\Z will match the first and last line, but you also have the rest of the contents
so a replacement of
will reconstruct the original file.
In both cases replacing the backreferences or adding any content will amend the file. With these adding something before/after the first/last line is a very simple replacement, for example with the second one:
Insert before first line:
Insert after first line:
Insert before last line:
Insert after last line:
Note for non-Sublime users: this regex requires multi-line mode due to the usage of