Nicolas Pierre
Nicolas Pierre

Reputation: 1195

Regex function matching

I'm playing around with regex to learn em a little bit and i came up with a problem i don't understand.

I have this regex

\s+(public|private|protected|internal|sealed).*[^{.}]\(.*

too match a line of function declaration in c#. But the thing is it works but also matches this line

private bool FooBar { get { return _fooBar != null && !_fooBar.BarFoo.Any(); } }

I tried to solve it with adding this condition in the regex [^{.}] but it keeps matching. So can someone help me out here ?

Upvotes: 0

Views: 71

Answers (1)

Lucero
Lucero

Reputation: 60190

The .* you have before your new condition matches all the stuff including the curly braces.

Therefore, you may want something like this:

\s+(public|private|protected|internal|sealed)\s+[^{.}]+\(

That being said, depending on what you want to do in the end a real parser instead of regular expressions may be more appropriate.

Upvotes: 2

Related Questions