[latex3-commits] [git/LaTeX3-latex3-latex3] main: Error when referring to inexistent group in a regex replacement (939f02d61)

Bruno Le Floch blflatex at gmail.com
Tue Apr 27 16:38:10 CEST 2021


Repository : https://github.com/latex3/latex3
On branch  : main
Link       : https://github.com/latex3/latex3/commit/939f02d613bfb4b6c2a547cbb6229f149ffad0fb

>---------------------------------------------------------------

commit 939f02d613bfb4b6c2a547cbb6229f149ffad0fb
Author: Bruno Le Floch <blflatex at gmail.com>
Date:   Tue Apr 27 16:38:10 2021 +0200

    Error when referring to inexistent group in a regex replacement
    
    Previously \regex_replace_all:nnN { ((a)b)c } { \7 } \l_tmpa_tl would
    silently ignore \7 since there were fewer than 7 groups in the regex


>---------------------------------------------------------------

939f02d613bfb4b6c2a547cbb6229f149ffad0fb
 l3kernel/CHANGELOG.md             |  2 ++
 l3kernel/l3regex.dtx              |  5 +++++
 l3kernel/testfiles/m3regex002.tlg |  9 +++++++++
 l3kernel/testfiles/m3regex003.tlg |  9 +++++++++
 l3kernel/testfiles/m3regex011.tlg | 18 ++++++++++++++++++
 5 files changed, 43 insertions(+)

diff --git a/l3kernel/CHANGELOG.md b/l3kernel/CHANGELOG.md
index 8b4473e03..ff89871f7 100644
--- a/l3kernel/CHANGELOG.md
+++ b/l3kernel/CHANGELOG.md
@@ -21,6 +21,8 @@ this project uses date-based 'snapshot' version identifiers.
 - Improve handling of `\exp_not:n` in `\text_expand:n` (issue #875)
 - `\file_full_name:n` now avoids calling `\pdffilesize` multiple times on the same file.
 - Show printable characters explicitly in `\regex_show:n`
+- Regex replacement now errors when using a submatch (`\1` etc) for which
+  the regex has too few groups
 
 ### Fixed
 - Evalutate integer constants only once (issue#861)
diff --git a/l3kernel/l3regex.dtx b/l3kernel/l3regex.dtx
index 2a6a95a7c..24e6cfb65 100644
--- a/l3kernel/l3regex.dtx
+++ b/l3kernel/l3regex.dtx
@@ -5576,6 +5576,9 @@
   {
     \if_int_compare:w #1 < \l_@@_capturing_group_int
       \@@_replacement_put_submatch_aux:n {#1}
+    \else:
+      \__kernel_msg_expandable_error:nnff { regex } { submatch-too-big }
+        {#1} { \int_eval:n { \l_@@_capturing_group_int - 1 } }
     \fi:
   }
 \cs_new_protected:Npn \@@_replacement_put_submatch_aux:n #1
@@ -7260,6 +7263,8 @@
     missing~right~
     \int_compare:nTF { #1 = 1 } { parenthesis } { parentheses } .
   }
+\__kernel_msg_new:nnn { regex } { submatch-too-big }
+  { Submatch~#1~used~but~regex~only~has~#2~group(s) }
 %    \end{macrocode}
 %
 % Some escaped alphanumerics are not allowed everywhere.
diff --git a/l3kernel/testfiles/m3regex002.tlg b/l3kernel/testfiles/m3regex002.tlg
index 1113a2204..430fc6603 100644
--- a/l3kernel/testfiles/m3regex002.tlg
+++ b/l3kernel/testfiles/m3regex002.tlg
@@ -42,6 +42,15 @@ Y
 TEST 4: replace all
 ============================================================
 How m(a)ny occurrences (a)re there?
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Submatch 7 used but regex only has 0 group(s)
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
 ^How^ ^many^ ^occurrences^ ^are^ ^there^?
 [How How][many many][occurrences occurrences][are are][there?there]
 03-10-2011,04-11-2011
diff --git a/l3kernel/testfiles/m3regex003.tlg b/l3kernel/testfiles/m3regex003.tlg
index c7fa35929..ab19fe690 100644
--- a/l3kernel/testfiles/m3regex003.tlg
+++ b/l3kernel/testfiles/m3regex003.tlg
@@ -5,6 +5,15 @@ Author: Bruno Le Floch
 TEST 1: Replace all
 ============================================================
 macro:->x[rstuvwuvwrst,rst,s,v]y[uvw,uvw,,v]r
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Submatch 3 used but regex only has 2 group(s)
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
 macro:->x[rstuvwuvwrst,s,v,]y[uvw,,v,]r
 macro:->(,)b(,)c(,)d(,)e(,)
 macro:->(a,)(,)b(,)
diff --git a/l3kernel/testfiles/m3regex011.tlg b/l3kernel/testfiles/m3regex011.tlg
index 6a2673df6..bd7120b17 100644
--- a/l3kernel/testfiles/m3regex011.tlg
+++ b/l3kernel/testfiles/m3regex011.tlg
@@ -9,6 +9,24 @@ TRUE
 ============================================================
 TEST 2: No groups within \c 
 ============================================================
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Submatch 2 used but regex only has 1 group(s)
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
+! Undefined control sequence.
+<argument> \LaTeX3 error: 
+                           Submatch 3 used but regex only has 1 group(s)
+l. ...  }
+The control sequence at the end of the top line
+of your error message was never \def'ed. If you have
+misspelled it (e.g., `\hobx'), type `I' and the correct
+spelling (e.g., `I\hbox'). Otherwise just continue,
+and I'll forget about whatever was undefined.
 > \l_tmpa_tl=\aa \bb ,\bb ,,.
 <recently read> }
 l. ...  }





More information about the latex3-commits mailing list.