How can I avoid this regex error when attempting to use texdef?

the perl code needs to be updated at the points indicated (lines 391 and lines 394 to use \{ and \} not { and }

    elsif ($macrodef =~ /^\\\@protected\@testopt \{?\\.*? \}? *(\\\\.*?) /) {
        unshift @cmds, $1;
    }
    elsif ($macrodef =~ /^\\\@testopt \{?(\\.*?) \}?/) {

This should be reported as a bug in texdef


Do not use this solution if you install TeX Live using packages distributed by a Linux distro, you are a Windows user or doing so requires root privileges and you are not sure what you're doing.

This is merely David's answer in the form of a patch for easy application, since it seems that this is not likely to get updated any time soon and updating TeX Live overwrites corrections to the original file.

--- 2017/texmf-dist/scripts/texdef/texdef.pl.orig       2016-11-25 18:32:54.000000000 +0000
+++ 2017/texmf-dist/scripts/texdef/texdef.pl    2017-08-27 22:49:49.052819126 +0100
@@ -388,10 +388,10 @@
             my $protectedmacro = $2;
             unshift @cmds, $protectedmacro;
         }
-        elsif ($macrodef =~ /^\\\@protected\@testopt {?\\.*? }? *(\\\\.*?) /) {
+        elsif ($macrodef =~ /^\\\@protected\@testopt \{?\\.*? \}? *(\\\\.*?) /) {
             unshift @cmds, $1;
         }
-        elsif ($macrodef =~ /^\\\@testopt {?(\\.*?) }?/) {
+        elsif ($macrodef =~ /^\\\@testopt \{?(\\.*?) \}?/) {
             unshift @cmds, $1;
         }
     }
@@ -679,7 +679,7 @@
             | (?:new|renew|provide)robustcmd\s* \*? \s* {? \s* \\  # etoolbox definitions
             | (?:new(?:box|count|dimen|if|insert|read|skip|muskip|toks|write)) \s* \\ # TeX registers etc.
             | (?:char|count|dimen|mathchar|skip|toks)def \s* \\  # TeX chardefs etc.
-            | \@namedef{?                                        # Definition by name only
+            | \@namedef\{?                                        # Definition by name only
             | Declare[a-zA-z]+ \s* \*? \s* {? \s* \\             # Declare... definitions
             | declare[a-zA-z]+ \s* \*? \s* {? \s* \\             # declare... definitions
         )

Save the file as, say, texdef.patch.

Assuming that you installed TeX Live from upstream on a Unix-ish system in the default location, so that texdef.pl is located at /usr/local/texlive/2017/texmf-dist/scripts/texdef/texdef.pl, put the patch file in /usr/local/texlive by navigating to the directory where you saved the file.

mv texdef.patch /usr/local/texlive

Then apply the patch using

cd /usr/local/texlive
patch -b -p0 <texdef.patch

If you installed TeX Live with root privileges, you'll need to su or use sudo. If you installed as yourself, you won't need anything. If you installed as, say, the texlive user (as explained in my instructions for installing as an unprivileged user with no access to your own home), you'll need to su texlive.

The -b flag tells patch to backup the file before patching and patch will not touch a file unless it can find a good match for the search text. So this should be relatively safe. At worst, it should simply fail to do anything. However, anything you do as root is only as safe as you make it, so I wouldn't do this as root unless you are confident you won't do anything too catastrophic.