def remove_comments(code):
    """Remove C-style comment from GLSL code string."""

    pattern = r"(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*\n)"
    # first group captures quoted strings (double or single)
    # second group captures comments (//single-line or /* multi-line */)
    regex = re.compile(pattern, re.MULTILINE | re.DOTALL)

    def do_replace(match):
        # if the 2nd group (capturing comments) is not None,
        # it means we have captured a non-quoted (real) comment string.
        if match.group(2) is not None:
            return ""  # so we will return empty to remove the comment
        else:  # otherwise, we will return the 1st group
            return match.group(1)  # captured quoted-string

    return regex.sub(do_replace, code)
def remove_comments ( code ) : pattern = r"(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*\n)" regex = re . compile ( pattern , re . MULTILINE | re . DOTALL ) def do_replace ( match ) : if match . group ( 2 ) is not None : return "" else : return match . group ( 1 ) return regex . sub ( do_replace , code )
Remove C-style comment from GLSL code string.