I think you simply want:
$attach1 =~ s/\?.*$//;
But you seem to be doing this in an awfully complicated way, and your parsing code is fragile (it will fail on:
http://www.perlmonks.com/index.pl?node=Smokers/Jokers
, for example) Have you considered the
URI module?
Update: I see why you'd want to use $ENV{QUERY_STRING}. The problem with your code is that the double-quotes are eating the backslash---by the time the regex sees the question mark it's unbackslashed, and so doesn't make any sense. Single quotes won't do that (at least not with a question mark). You're also missing the replacement string on your regex. This code works for me:
$paramstr = '\?'.$ENV{QUERY_STRING};
$attach1 =~ s/$paramstr//;
But it fails if there are slashes in the query part of the string.