Originally Posted By: tfabris
Thanks so much for your excellent examples! You clearly understand regex deeply, and are therefore my new best friend.

Heh. I lurked on comp.lang.perl.misc for a decade. smile

Quote:
But it has another issue to solve:

In addition to the function I want to scan, it also returns the introductory lines of the next function (the HTML comments and the test decorators). I want it to *not* include those lines, I want it to stop at the closing curlybrace just BEFORE those lines. I don't know how to do the regex syntax to pull off that trick. [...] Do you know how to do that?

I'm not certain this is be possible with git/POSIX expressions. What you need is to say "match } followed by a newline followed by anything (including newlines) that's not a } followed by something in this set of keywords." That will get you the last } character immediately preceding one of your special keywords.

In PCRE, that would be:
Code:
/}\n[^}]*?(?:keyword|list)/ms

The problem is that git compiles the regex with the REG_NEWLINE flag, so '.' and other match-any-character operators (such as [^}]) will not match a newline. Making matters worse, POSIX regex (even the extended regex) does not define any escape characters, so you can't even explicitly say "match a newline" aside from using the ^ and $ operators (which technically don't match newlines, but match the empty boundary immediately following or preceding the newline). The net effect is that can't do any matches that cross line boundaries.

Quote:
Something else about your example: the function I'm scanning might, in some cases, be the very last function in the file. In that case, your example gets the error message:
Code:
fatal: -L parameter '\(public\|private\|protected\|internal\|sealed\)' starting at line 638: No match

What is the syntax to work around that so that it gets me to the end of the file even if it can't find the (public|private|etc.)?

In this case, you have to supply two -L arguments. The first is the one you just made, the other is:
Code:
-L '/MethodName/,'

So you're not specifying an end to the range.

Quote:
I also thought of a way to make it more robust. The functions I'm scanning should always be an Nunit test, and by definition, nunit tests must be declared "public void", so I could include that before the name of the function in the first part of the regex so that I can be certain I'm finding the actual test function.

That's a good addition.