#!/usr/bin/perl -i.bak

use English;

## get input
undef $INPUT_RECORD_SEPARATOR;
$file = <>;

## transform it
my $alignOrEqnarrayRE = '(?:align|eqnarray)\\*?';

$file =~ s/(\\begin{($alignOrEqnarrayRE)}\n?)(.*?)(\\end{\2})/"$1".doBlock($3)."$4"/egs;


# print output
print $file; 



sub doBlock {
    my ($block) = @_; 

    my $notEqualsNotDoubleBackslashCharRE = '((?!\\\\\\\\).)*?';
    $block =~ s/(?<=\n)($notEqualsNotDoubleBackslashCharRE\n)/\\\\$1/gs;


    return $block;
}



