none
How to usd regular expression to match a function in the .cs file?

    Question

  • I want to find the function named "myFunction()"
    in my cs file, then show the source code of it:
    public void myFunction()
    {
       ...
       ...
    }

    I wrote a regex :
    ^\s*([a-zA-Z]+\s+)+Fun\(.*\)\s+\{.*(\{[^\{\}]*\})*\}
    but it can't work.
    could you give me a sample ?

    Thanks
    Thursday, November 17, 2005 6:00 AM

Answers

  • Ah, very interesting.  I was not aware of the <DEPTH> construct.  Then, I suppose, it may be possible.  I do not have such a Regex specification on hand, though.  I imagine most C# text parsers (e.g. compilers) do not use regular expressions to piece out function definitions.  Note also that you'd need to not only ignore lines beginning with //, but items between /* and */; furthermore, quotation constructs need to be ignored in all varieties.  This may prove quite tricky. :)
    Monday, December 05, 2005 6:11 PM
    Moderator

All replies

  • It cannot be done.  You would need, at the very least, a balanced-paren matcher (even without ignoring comments), and that is, by definition, not regular, so no regular expression package in the world will be able to do this.  You can easily roll your own, and we could help you with that.

    You COULD match function NAMES, though, looking for keywords like "public", "static", "void", etc. 

    Wednesday, November 23, 2005 4:08 PM
    Moderator
  • Actually, you can do balanced brace matching using the "balancing group definition". This subject is really under-documented in MSDN, but page 430 of this book can help out.

    The below fragment can match a method, but doesn't take comments into consideration yet (which aren't hard since there's no balancing involved, but it would make it a lot more tedious and ugly):



    Regex regex = new Regex(@"
        \bmyFunction\s* # MethodName
        \((?>[^)]*)\) # (...params...)
        \s+
        \{
        (?>
            [^{}]+ # Code
        |
            \{ (?<DEPTH>) # Opening bracket
        |
            \} (?<-DEPTH>) # Closing bracket
        )*
        (?(DEPTH)(?!)) # All brackets must be closed
        \}", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);

     

    Saturday, November 26, 2005 10:34 PM
  • I don't know if I replied to this before, but it is impossible to write such an expression.  Strings with matched parens (or braces, etc.), where the number of matches is unspecified and unbounded (like C#) is a canonical example of a non-regular formal language, and this is the minimum requirement for matching a function name and body.   
    Friday, December 02, 2005 8:26 PM
    Moderator
  • Right right, you can't do that with your ordinary regular expressions. But .NET's regular expressions have been extended in some points, this being one of them. Quoting the document I linked in my previous post: "Microsoft has included an interesting innovation for matching balanced constructs - historically, something not possible with a regular expression". Have a look at it, the document makes an interesting read.
    Friday, December 02, 2005 10:41 PM
  • Ah, very interesting.  I was not aware of the <DEPTH> construct.  Then, I suppose, it may be possible.  I do not have such a Regex specification on hand, though.  I imagine most C# text parsers (e.g. compilers) do not use regular expressions to piece out function definitions.  Note also that you'd need to not only ignore lines beginning with //, but items between /* and */; furthermore, quotation constructs need to be ignored in all varieties.  This may prove quite tricky. :)
    Monday, December 05, 2005 6:11 PM
    Moderator