From fc447b46cea99c309928b62a44f3373f369baada Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 20 Feb 2022 13:28:13 +1100 Subject: [PATCH] Fixed bad cature groups with multiple matches #1114 --- pkg/yqlib/doc/operators/string-operators.md | 18 ++++++++++++------ pkg/yqlib/operator_strings.go | 8 ++++---- pkg/yqlib/operator_strings_test.go | 6 +++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/yqlib/doc/operators/string-operators.md b/pkg/yqlib/doc/operators/string-operators.md index a13288fa..07c04a79 100644 --- a/pkg/yqlib/doc/operators/string-operators.md +++ b/pkg/yqlib/doc/operators/string-operators.md @@ -105,14 +105,14 @@ will output captures: [] ``` -## Match with capture groups +## Match with global capture group Given a sample.yml file of: ```yaml abc abc ``` then ```bash -yq '[match("(abc)+"; "g")]' sample.yml +yq '[match("(ab)(c)"; "g")]' sample.yml ``` will output ```yaml @@ -120,16 +120,22 @@ will output offset: 0 length: 3 captures: - - string: abc + - string: ab offset: 0 - length: 3 + length: 2 + - string: c + offset: 2 + length: 1 - string: abc offset: 4 length: 3 captures: - - string: abc + - string: ab offset: 4 - length: 3 + length: 2 + - string: c + offset: 6 + length: 1 ``` ## Match with named capture groups diff --git a/pkg/yqlib/operator_strings.go b/pkg/yqlib/operator_strings.go index 0cd8f8f5..d57fbeb6 100644 --- a/pkg/yqlib/operator_strings.go +++ b/pkg/yqlib/operator_strings.go @@ -136,19 +136,19 @@ func match(matchPrefs matchPreferences, regEx *regexp.Regexp, candidate *Candida } for i, matches := range allMatches { - capturesNode := &yaml.Node{Kind: yaml.SequenceNode} + capturesListNode := &yaml.Node{Kind: yaml.SequenceNode} match, submatches := matches[0], matches[1:] for j, submatch := range submatches { captureNode := &yaml.Node{Kind: yaml.MappingNode} - captureNode.Content = addMatch(capturesNode.Content, submatch, allIndices[i][2+j*2], subNames[j+1]) - capturesNode.Content = append(capturesNode.Content, captureNode) + captureNode.Content = addMatch(captureNode.Content, submatch, allIndices[i][2+j*2], subNames[j+1]) + capturesListNode.Content = append(capturesListNode.Content, captureNode) } node := &yaml.Node{Kind: yaml.MappingNode} node.Content = addMatch(node.Content, match, allIndices[i][0], "") node.Content = append(node.Content, createScalarNode("captures", "captures"), - capturesNode, + capturesListNode, ) results.PushBack(candidate.CreateReplacement(node)) diff --git a/pkg/yqlib/operator_strings_test.go b/pkg/yqlib/operator_strings_test.go index c4e26214..8a510993 100644 --- a/pkg/yqlib/operator_strings_test.go +++ b/pkg/yqlib/operator_strings_test.go @@ -30,11 +30,11 @@ var stringsOperatorScenarios = []expressionScenario{ }, }, { - description: "Match with capture groups", + description: "Match with global capture group", document: `abc abc`, - expression: `[match("(abc)+"; "g")]`, + expression: `[match("(ab)(c)"; "g")]`, expected: []string{ - "D0, P[], (!!seq)::- string: abc\n offset: 0\n length: 3\n captures:\n - string: abc\n offset: 0\n length: 3\n- string: abc\n offset: 4\n length: 3\n captures:\n - string: abc\n offset: 4\n length: 3\n", + "D0, P[], (!!seq)::- string: abc\n offset: 0\n length: 3\n captures:\n - string: ab\n offset: 0\n length: 2\n - string: c\n offset: 2\n length: 1\n- string: abc\n offset: 4\n length: 3\n captures:\n - string: ab\n offset: 4\n length: 2\n - string: c\n offset: 6\n length: 1\n", }, }, {