From 19752f50a95df704bf4b19e68996a74758caee75 Mon Sep 17 00:00:00 2001 From: Nathan Chandrasekar <322284620+vera-rubin@users.noreply.github.com> Date: Sat, 5 Dec 2026 19:23:51 -0500 Subject: [PATCH] Fix capture and sub with duplicate group names (#3676) When a regex had several groups with the same name, a later group that did not take part in the match overwrote an earlier one that did, so capture returned null and sub interpolated null instead of the matched text. Build the capture object in capture/2 and in the sub reducer by adding a one-key object for each named group, keeping the value already stored for that name when the current group did not match. The last group of each name that took part in the match now wins, empty-string matches are kept, and the value stays null only when no group of that name matched. Key order is unchanged. Add a regression test for capture, sub and gsub. --- src/builtin.jq | 6 +++--- tests/onig.test | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/builtin.jq b/src/builtin.jq index f5b53c55..96a7a124 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -90,7 +90,7 @@ def test($val): ($val|type) as $vt | if $vt == "string" then test($val; null) elif $vt == "array" and ($val | length) > 1 then test($val[0]; $val[1]) elif $vt == "array" and ($val | length) > 0 then test($val[0]; null) else error( $vt + " not a string or array") end; -def capture(re; mods): match(re; mods) | reduce ( .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair ({}; . + $pair); +def capture(re; mods): match(re; mods) | reduce ( .captures | .[] | select(.name != null) ) as {$name, $string} ({}; . + {($name): ($string // .[$name])}); def capture($val): ($val|type) as $vt | if $vt == "string" then capture($val; null) elif $vt == "array" and ($val | length) > 1 then capture($val[0]; $val[1]) elif $vt == "array" and ($val | length) > 0 then capture($val[0]; null) @@ -120,8 +120,8 @@ def sub($re; s; $flags): ({result: [], previous: 0}; $in[ .previous: ($edit | .offset) ] as $gap # create the "capture" objects (one per item in s) - | [reduce ( $edit | .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair - ({}; . + $pair) | s ] as $inserts + | [reduce ( $edit | .captures | .[] | select(.name != null) ) as {$name, $string} + ({}; . + {($name): ($string // .[$name])}) | s ] as $inserts | reduce range(0; $inserts|length) as $ix (.; .result[$ix] += $gap + $inserts[$ix]) | .previous = ($edit | .offset + .length ) ) | .result[] + $in[.previous:] ) diff --git a/tests/onig.test b/tests/onig.test index 97374f45..ba6c2f3e 100644 --- a/tests/onig.test +++ b/tests/onig.test @@ -99,6 +99,11 @@ capture("(?[a-z]+)-(?[0-9]+)") "xyzzy-14" {"a":"xyzzy","n":"14"} +# duplicate group names retain matched strings, including empty strings +"(?^$)|(?a)|(?b)|c" as $re | map([capture($re), sub($re; "<\(.x)>"), gsub($re; "<\(.x)>")]) +["a","b","","c","ab"] +[[{"x":"a"},"",""],[{"x":"b"},"",""],[{"x":""},"<>","<>"],[{"x":null},"",""],[{"x":"a"},"b",""]] + # jq-coded utilities built on match: #