Your regular expression is quoted with single quotes, but it also contains a single quote.
The single quote in ["']
needs to be escaped, or it will signal the end of the quoted string to the shell.
This will fix it:
grep -r -P -o -h '(?<=(?<!def )my_method )(["'\''])(?:(?=(\\?))\2.)*?\1'
# ^^^^
With ["'\'']
, the first '
ends the first part of the string, the \'
inserts a literal single quote, and the last '
starts a new single quoted string that will be concatenated with the previous bits. Only the middle single quote will end up in the regular expression itself, the other two will be removed by the shell.
(["\x27])
will work fine (replacing the single quote with it's ascii code) – George Vasiliou Apr 03 '17 at 12:23\x27
is as many characters as'\''
. – Kusalananda Apr 03 '17 at 12:24'\''
is transferable to other non-regex situations whereas\x27
is not. – Muzer Apr 03 '17 at 14:41