This might work:
^#?(\$\w+)\s*=.*[\r\n]+#?\1\s*=.*$
Explanation:
^ start of line
(\$\w+) $, followed by variable name, captured into backreference no. 1
\s* optional whitespace
= =
.* any characters except newline
[\r\n]+ one or more newlines (\r thrown in for safety)
\1 same variable name as in the last line
\s*=.* as above
$ end of line
This does not verify that exactly one of the lines is commented out (it will also match if neither, nor both). If this is a problem, let me know.
I don't know if grep can match multiple lines in one regex; any tool you use should be set to ^/$match start / end of line mode (instead of beginning / end of line).
source
share