Visual Studio regex to remove all comments and blank lines in VB.NET code using a macro

后端 未结 3 1600
长情又很酷
长情又很酷 2021-01-14 00:57

I was trying to remove all comments and empty lines in a file with the help of a macro. Now I came up with this solution which deletes the comments(there is some bug descri

3条回答
  •  自闭症患者
    2021-01-14 01:04

    I've just checked with the two examples from above, '+{.+}$ should do. Optionally, you could go with ('|'')+{.+}$ but the first solution also replaces the xml-descriptions ).

    ''' 
    ''' Method Description
    ''' 
    ''' 
    Sub Main()
        ''first comment
        Dim a As String = "" 'second comment
    End Sub
    

    Edit: if you use ('+{.+}$|^$\n) it deletes a) all comments and b) all empty lines. However, if you have a comment and a End Sub/Function following, it takes it up one line which results in a compiler error.

    Before

        ''' 
        ''' 
        ''' 
        ''' 
        Sub Main()
            ''first comment
            Dim a As String = "" 'second comment
    
        End Sub
    
        ''' 
        ''' 
        ''' 
        ''' 
        ''' 
        Public Function asdf() As String
            Return "" ' returns nothing
    
        End Function
    

    After

    Sub Main()
        Dim a As String = ""
    End Sub
    
    Public Function asdf() As String
        Return ""         
    End Function
    

    Edit: To delete any empty lines Search Replace the following regex ^$\n with empty.

提交回复
热议问题