Using this regular expression to find methods of C#. \w+(([^)]+))|\w+(()) with this I get the method name and parameters, I need to include the character "{" to be sure that is the definition of a method. View Image
Asked
Active
Viewed 58 times
0
-
Then add a `\s*?{` at the end of the expression to match the whitespace and the curly brace? – Maximilian Gerhardt Dec 28 '15 at 20:14
-
You cannot just append that to the pattern end, the alternatives should be placed inside a non-capturing group. – Wiktor Stribiżew Dec 28 '15 at 20:38
-
Finally, the pattern is (public|protected|internal|private)\s+?[\w+\s]+\w+\(([^)]+)\)|(public|protected|internal|private)\s+?[\w+\s]+\w+\(()\). In c# the brace is a break down when is the code formated. thks a lot!!! – JymmyT Dec 28 '15 at 22:13
-
Have you considered using the compiler objects to parse the code for you? You'll never handle all the formatting cases with a simple regex. – SledgeHammer Dec 02 '16 at 22:52
1 Answers
2
You can place the pattern into a non-capturing group ((?:...)
) and then add {
:
(?:\w+\(([^)]+)\)|\w+\(()\))[\p{Zs}\t]*{
^^^ ^ ^
See demo
Note that [\p{Zs}\t]*
matches 0 or more horizontal whitespace symbols in .NET.
Also note that {
does not have to be escaped in a .NET regex.

Wiktor Stribiżew
- 607,720
- 39
- 448
- 563
-
Note that you do not really have to use `()` to capture empty string. I kept it just in case you are using it somewhere in your code. – Wiktor Stribiżew Dec 28 '15 at 20:36
-
-
Like [`(public|protected|internal|private)\s+\w+\s+(?:\w+\(([^)]+)\)|\w+\(()\))\p{Zs}*{`](https://regex101.com/r/iG3bQ8/3)? – Wiktor Stribiżew Dec 28 '15 at 21:30