There is a subtle issue here:
when using 2 addresses, and the second address is an RE,
the first line matching the first address (line 2 in your case)
will *never* be checked against the RE.
And so, even though the '//!d' is run conditionally,
the condition is true (line 2, before regex is checked),
and then '//!d' is executed but there is no 'last regex' yet.
This is documented in the manual:
https://www.gnu.org/software/sed/manual/sed.html#Range-Addresses
(in the second paragraph, starting with "if the second address is a regexp").
Also,
In the POSIX standard, the relevant text is:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
(under "addresses in sed" section).
"An editing command with two addresses shall select the inclusive
range from the first pattern space that matches the first address
through the next pattern space that matches the second. "
The interpretation is that the second address is checked against
"the next pattern space" - impling that the first time the second
address is checkd is not in the first line that matches, but
on the 'next pattern space' (meaning starting at the line following
the line that matched the first address).
[phew, that is a bit confusing....]
Does this clarify the issue?
-assaf