I think preg_match ('@<code>([.\n])</code>@',$text,$match); will solve your problem.
I do not think so. In the first place, [man]preg_replace/man is more appropriate. I would suggest something along these lines:
$text = preg_replace('@<code>(.*?)</code>@me',
'"<code>" . htmlspecialchars("$1") . "</code>"', $text);