We already have some answers explaining the issue. Let me give a solution to your problem. Let's say you have the code
RegularExpression["((re)*)"]
that you want to comment out. Since we have nested comments in Mathematica, just use a pair of (*
to prevent your issue:

Although I don't know the internal implementation of Mathematicas parser, the reason why this goes wrong is pretty universal. Parsing is usually a two-step process. First, the input is broken down into tokens by a so-called lexer and comments are often separated from the rest of the code in this stage. Taking your example
(* RegularExpression["((re)*)"] *)
this means the lexer first reads the token COMMENT_OPEN
. Then, the lexer usually reads over everything until it meets the closing *)
. And here it is important to know that the lexer really stupidly reads over the input.
Unfortunately, the next closing *)
is inside the stuff you wanted to comment out. At this point the lexer leaves its stupid comment mode again and tries to tokenize real code. Now, he is in the middle of your expression and everything goes wrong.
Btw, using the fultzTokenize
of this answer shows how Mathematica breaks your code down. Note that I had to quote the quotes accordingly. See that strings are kept together when the lexer is in code mode when going over str2
:
str1="(* RegularExpression[\"((re)*)\"] *)";
str2="RegularExpression[\"((re)*)\"]";
fultzTokenize/@{str1,str2}
{(*, ,RegularExpression,[,"((re),*),"] *)}
{ RegularExpression,[,"((re)*)",]}
RegularExpression["((re)*"<>")"]
$\endgroup$ – ybeltukov Jan 22 '15 at 11:19