Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression needed to remove C/C# comments

Tags:

c#

regex

I need a C# regex to delete everything between /* and */ including the /**/. So, basically remove all code comments in the given text.

like image 960
Andrej Avatar asked May 26 '11 12:05

Andrej


People also ask

What does ?= Mean in regular expression?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

Does C have regular expression?

A regular expression is a sequence of characters used to match a pattern to a string. The expression can be used for searching text and validating input. Remember, a regular expression is not the property of a particular language. POSIX is a well-known library used for regular expressions in C.

What is the regex for special characters?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "."

Why * is used in regex?

- a "dot" indicates any character. * - means "0 or more instances of the preceding regex token"


1 Answers

Should be something like this:

var regex = new Regex("/\*((?!\*/).)*\*/", RegexOptions.Singleline);

regex.Replace(input, "");
like image 188
petho Avatar answered Sep 19 '22 02:09

petho