Search code examples
regexvb.netregex-groupregex-greedy

How to filter out c-type comments with regex?


I'm trying to filter out "c-style" comments in a line so i'm only left with the words (or actual code).

This is what i have so far: demo

regex:

\/\*[^\/]*[^\*]*\*\/

text:

/* 1111 */ one /*2222*/two /*3333 */ three/* 4444*/ four /*/**/ five /**/

Solution

  • We can try doing a regex replacement on the following pattern:

    /\*.*?\*/
    

    This matches any old-school C style comment. It works by using a lazy dot .*? to match only content within a single comment, before the end of that comment. We can then replace with empty string, to effectively remove these comments from the input.

    Code:

    Dim input As String = "/* 1111 */ one /*2222*/two /*3333 */ three/* 4444*/ four /*/**/ five /**/"
    Dim output As String = Regex.Replace(input, "/\*.*?\*/", "")
    Console.WriteLine(input)
    Console.WriteLine(output)
    

    This prints:

    one two  three four  five