fix(strings): ignore unnamed capture groups

capture() built a map key from SubexpNames() for every submatch; unnamed groups (name "") became an empty-string key (and duplicate keys for several). jq returns only named captures.
This commit is contained in:
max 2026-07-06 03:55:05 +02:00
parent e2f1d5ccf7
commit 3a74b34bf1
2 changed files with 17 additions and 2 deletions

View File

@ -365,8 +365,14 @@ func capture(matchPrefs matchPreferences, regEx *regexp.Regexp, candidate *Candi
_, submatches := matches[0], matches[1:] _, submatches := matches[0], matches[1:]
for j, submatch := range submatches { for j, submatch := range submatches {
name := subNames[j+1]
keyNode := createScalarNode(subNames[j+1], subNames[j+1]) // jq only returns named capture groups; unnamed groups (whose
// SubexpNames entry is "") must be skipped so they don't become
// map entries with an empty-string key.
if name == "" {
continue
}
keyNode := createScalarNode(name, name)
var valueNode *CandidateNode var valueNode *CandidateNode
offset := allIndices[i][2+j*2] offset := allIndices[i][2+j*2]

View File

@ -364,6 +364,15 @@ var stringsOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[\"1\", \"true\", \"null\", \"~\", cat, \"{an: object}\", \"[array, 2]\"]\n", "D0, P[], (!!seq)::[\"1\", \"true\", \"null\", \"~\", cat, \"{an: object}\", \"[array, 2]\"]\n",
}, },
}, },
{
skipDoc: true,
description: "Capture ignores unnamed groups",
document: `foobar`,
expression: `capture("(foo)(?P<rest>bar)")`,
expected: []string{
"D0, P[], (!!map)::rest: bar\n",
},
},
} }
func TestStringsOperatorScenarios(t *testing.T) { func TestStringsOperatorScenarios(t *testing.T) {