Q: Why is this regex so long?
A: To handle the possibility of escaping a dollar sign in a variable name, as well as handle escaping a slash before a dollar sign in a variable name. Currently, typing '\\' into a Dockerfile will get turned into '\', but typing '\' will appear as '\', too. So:

    ENV $TEST test

    ENV NEW $TEST
    RUN echo $NEW
    #   test 

    ENV NEW \$TEST
    RUN echo $NEW 
    #   \$TEST

    ENV NEW \\$TEST
    RUN echo $NEW
    #   Although this is an escaped slash (\\) in the Dockerfile, by the time we have it, it has already become '\', meaning that it is still     escaped:
    #   \$TEST

    ENV NEW \\\$TEST
    RUN echo $NEW
    #   Here, the pair of slashes turns into one slash and the other slash stays the same, outputting:
    #   \\test

    ENV NEW \\\\$TEST
    RUN echo $NEW
    #   Here, both pairs of slashes turn into one slash, giving:
    #   \\test