mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-08 06:45:38 +00:00
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:
parent
e2f1d5ccf7
commit
3a74b34bf1
@ -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]
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user