I want to use ag to print the classes and their methods in a python file. I thought that this would be easy using:
ag --context=0 --nocolor -os '^\s*(def|class)\s+[_A-Za-z]*' prog.py
but for reasons I don't understand this is also matching blank lines. For example, if you make prog.py the following
class MyFavouriteClass def __init__ def __contains__ blah class MyNextFavouriteClass def _repr_ def __iter__
then it returns the full file, including the blank lines, except for the line containing
blah. Of course, I can always pipe the output into something else to remove the blank lines but I'd rather get it right the first time.
I suspect that the problem has nothing to do with the regular expression and, instead, that it's a feature of
--before flags but I can't find a combination of these that does what I want.