I want to replace /*
in selected text with //
.
I used regex to do this. When I used any other strings it worked. But when I used:
String result = System.Text.RegularExpressions.Regex.Replace(seltext,"/*","//");
It shows:
/* int a,b; // sample input
///*i//n//t//a//,//b//; // sample output
Instead I want:
// int a,b;
*
has a special meaning in regular expressions - it means "match 0 or more of the preceding character/group".
It sounds like you don't want a regex at all - you just want
string result = seltext.Replace("/*", "//");
If you really want to use regular expressions, you need to escape the *
(and various other characters, if you use them):
string result = Regex.Replace(seltext, @"/\*", "//");
Note the use of a verbatim string literal (indicated by the the @
at the start of the string) to avoid having to escape the \
as well for C# string literal reasons. You'd need to use "/\\*"
which isn't as clear. Verbatim string literals are very handy for regular expressions.
I would suggest caution when trying to use simple text operations (including regular expressions) on source code though. For example, imagine applying the replacement to the first of the code snippets above...
See more on this question at Stackoverflow