Need perl regular expression to remove the comment

I need a perl substitution to remove only the comment in the line . That line may have '#' with in double quotes .I used the following ,

 s/(^.*\".+?#.+?\".+?)(#.*)/$1/g

It works for ,

print " not a comment # not a comment " . "not a comment # not a comment" ;  # It is a comment 

but for the line like ,

print " not a comment # not a comment " . "not a comment # not a comment" ;

It gave the output ,

print " not a comment # not a comment " . "not a comment

It removes the text from the last '#' . Can any one give me the exact regular expression to solve this problem .

Thanks in advance .

can you please add the output for first line for which code is working and the expected output of the second line.

while(<DATA>){
  s/#(?=[^"]*$).*$//;
  print;
}
__DATA__
" not a comment # not a comment " . "not a comment # not a comment"
" not a comment # not a comment " . "not a comment # not a comment" ;  # It is a comment
 ruby -ne 'print if gsub(/;.*#.*/,";")' file