From 0bf621346bb7cd45f2289b71ab295f640e507786 Mon Sep 17 00:00:00 2001 From: Jeff Davis <14242442+JefeDavis@users.noreply.github.com> Date: Tue, 27 Jul 2021 13:21:07 -0500 Subject: [PATCH] fix(format): make regex for command search ungreedy and reset value on each instance regex for sed command search was greedy and match multiple markers if on the same line, change regex to not be greedy Closes #110, #111 Signed-off-by: Jeff Davis --- internal/actions/format.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/actions/format.go b/internal/actions/format.go index 3d612d1..f2c0c05 100644 --- a/internal/actions/format.go +++ b/internal/actions/format.go @@ -70,7 +70,7 @@ func doFormat(s string, values ...interface{}) string { } } - re := regexp.MustCompile(`(?P%[vklfh])(?P{(?P.*)})?`) + re := regexp.MustCompile(`(?P%[vklfh])(?P{(?P.*?)})?`) matches := re.FindAllStringSubmatch(s, -1) @@ -84,16 +84,18 @@ func doFormat(s string, values ...interface{}) string { } if result["marker"] != "" { + marker := valueMap[result["marker"]] + if result["command"] != "" { var err error - valueMap[result["marker"]], err = processSedCommand(result["command"], valueMap[result["marker"]]) + marker, err = processSedCommand(result["command"], valueMap[result["marker"]]) if err != nil { log.Warningf("Skipping additional format on [%s] due to invalid sed exp [%s], %s", s, result["command"], err) } } - s = strings.Replace(s, match[0], valueMap[result["marker"]], 1) + s = strings.Replace(s, match[0], marker, 1) } }