Subject: bug#16865: grep -wP and backreferences

From: Stephane Chazelas <[email-masked]>
Date: Mon, 24 Feb 2014 10:01:54 +0000

Hello,

Backreferences don't work with -w or -x in combination with -P:

$ echo aa | grep -Pw '(.)\1'
$

Or they work in an unexpected way:

$ echo aa | grep -Pw '(.)\2'
aa

The fix is simple:

--- src/pcresearch.c~	2014-02-24 09:59:56.864374362 +0000
+++ src/pcresearch.c	2014-02-24 07:33:04.666398105 +0000
@@ -75,9 +75,9 @@ Pcompile (char const *pattern, size_t si
 
   *n = '\0';
   if (match_lines)
-    strcpy (n, "^(");
+    strcpy (n, "^(?:");
   if (match_words)
-    strcpy (n, "\\b(");
+    strcpy (n, "\\b(?:");
   n += strlen (n);
 
   /* The PCRE interface doesn't allow NUL bytes in the pattern, so