2

I've created a regex which I need to run with grep, I'm pretty sure the regex is fine as it works with online regex tools, however when I run

grep -r -P -o -h '(?<=(?<!def )my_method )(["'])(?:(?=(\\?))\2.)*?\1'

I get the error Syntax error: ")" unexpected.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

10

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.

fedorqui
  • 7,861
  • 7
  • 36
  • 74
Kusalananda
  • 333,661
5

As @Kusalananda explained, the problem is the ' inside the regex. A simple solution is to use " for the regex since " can be escaped even inside a "-quoted string, unlike ' which cannot be escaped inside a '-quoted string:

grep -rPoh "(?<=(?<!def )my_method )([\"'])(?:(?=(\\?))\2.)*?\1"
terdon
  • 242,166