Regex to get VB6 function definitions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nassegris@gmail.com

    #1

    Regex to get VB6 function definitions

    Hello everyone!

    I'm trying to write a regular expression to capture VB6 function
    definitions and I'm abit stuck. The rules are:

    Function header:
    * Must contain the words SUB or FUNCTION
    * May contain the words PUBLIC or PRIVATE
    * May NOT contain the word DECLARE
    * Ends at the first newline that is not preceded by an underscore (_)
    followed by a space.

    Function footer
    * Must contain the words END FUNCTION or END SUB only

    Function exit:
    * Must contain the words EXIT FUNCTION or EXIT SUB only

    The idea is that I want to insert some logging code after each
    function header and some logging code before each exit or end. So what
    I need is a regex that would capture whole functions with groups for:
    * Whole function header
    * Function name
    * Function parameter list

    So far I've come up with
    (?:^\s*(?:publi c|private){0,1} \s(?:sub|functi on)\s) (?<FunctionName >[\w
    \d]+) (?<FunctionArgs >(?:\s*.*_\s$)* (?:\s*.*$) | (?:.*$)) # FUNCTION
    HEADER
    and
    (^\s*(?:end|exi t)\s(?:sub|func tion)).*$ # FUNCTION FOOTER

    but I don't really know how to combine them to get the complete
    function back with the exits and ends tagged properly.

    Could someone plese help me out with some tips?

    Regards,

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Regex to get VB6 function definitions

    To be honest, I don't know how you would use a regex for this, and I
    hope someone else can help you on that front.

    Given that you want the task of processing a number of vb files, have
    you thought about writing an add-in for the VB6 environment? You can do so
    in .NET, and access the specific components of the code files (functions,
    properties, etc, etc) through the object model. It might make it a lot
    easier than parsing it all apart using regular expressions.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <nassegris@gmai l.comwrote in message
    news:1186754345 .541492.137380@ m37g2000prh.goo glegroups.com.. .
    Hello everyone!
    >
    I'm trying to write a regular expression to capture VB6 function
    definitions and I'm abit stuck. The rules are:
    >
    Function header:
    * Must contain the words SUB or FUNCTION
    * May contain the words PUBLIC or PRIVATE
    * May NOT contain the word DECLARE
    * Ends at the first newline that is not preceded by an underscore (_)
    followed by a space.
    >
    Function footer
    * Must contain the words END FUNCTION or END SUB only
    >
    Function exit:
    * Must contain the words EXIT FUNCTION or EXIT SUB only
    >
    The idea is that I want to insert some logging code after each
    function header and some logging code before each exit or end. So what
    I need is a regex that would capture whole functions with groups for:
    * Whole function header
    * Function name
    * Function parameter list
    >
    So far I've come up with
    (?:^\s*(?:publi c|private){0,1} \s(?:sub|functi on)\s) (?<FunctionName >[\w
    \d]+) (?<FunctionArgs >(?:\s*.*_\s$)* (?:\s*.*$) | (?:.*$)) # FUNCTION
    HEADER
    and
    (^\s*(?:end|exi t)\s(?:sub|func tion)).*$ # FUNCTION FOOTER
    >
    but I don't really know how to combine them to get the complete
    function back with the exits and ends tagged properly.
    >
    Could someone plese help me out with some tips?
    >
    Regards,
    >

    Comment

    Working...