xralf
xralf

Reputation: 3642

grep capture python function definition

I have tried:

$ grep -Po "def[[:space:]]+\K(.*)\(" code.py

The output should be a list of function names.

I think it should be correct, but it's only capturing one extra bracket. I would like to get rid off it in the expression (not postprocessing). Please, explain the syntax, you will use.

Upvotes: 0

Views: 277

Answers (1)

jordanm
jordanm

Reputation: 34924

You can omit the "(" from the resulting string by putting it in a lookahead (?=<expression>):

grep -Po "def[[:space:]]+\K(.*)(?=\()" code.py

Upvotes: 1

Related Questions