From 517518dd30649228a92ae5e165ca53c2a49da9f4 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 15 Aug 2022 14:29:43 +0200 Subject: [PATCH] trim square brackets in SquareBracketsLanguageParserRegex --- language_parsing.go | 2 +- utils.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/language_parsing.go b/language_parsing.go index 90fbe69..1ecdbcb 100644 --- a/language_parsing.go +++ b/language_parsing.go @@ -7,5 +7,5 @@ type LanguageParserFunc func(str string) []string var SquareBracketsLanguageParserRegex = regexp.MustCompile(`\[.+?\]`) func SquareBracketsLanguageParser(str string) []string { - return SquareBracketsLanguageParserRegex.FindAllString(str, -1) + return TrimPrefixSuffix(SquareBracketsLanguageParserRegex.FindAllString(str, -1), "[", "]") } diff --git a/utils.go b/utils.go index 807ff76..e60493b 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,9 @@ package parsers -import "strconv" +import ( + "strconv" + "strings" +) func atoi(s string) (int, bool) { v, err := strconv.Atoi(s) @@ -9,3 +12,11 @@ func atoi(s string) (int, bool) { } return v, true } + +func TrimPrefixSuffix(arr []string, prefix, suffix string) []string { + trims := make([]string, 0, len(arr)) + for _, str := range arr { + trims = append(trims, strings.TrimSuffix(strings.TrimPrefix(str, prefix), suffix)) + } + return trims +}