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

How to usd regular expression to match a function in the .cs file?
albertkhor
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.
Stefan W.
Richard_C
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);
rvaneijk
Kollega