trim square brackets in SquareBracketsLanguageParserRegex

This commit is contained in:
Timon Ringwald 2022-08-15 14:29:43 +02:00
parent d4e5e65935
commit 517518dd30
2 changed files with 13 additions and 2 deletions

View File

@ -7,5 +7,5 @@ type LanguageParserFunc func(str string) []string
var SquareBracketsLanguageParserRegex = regexp.MustCompile(`\[.+?\]`) var SquareBracketsLanguageParserRegex = regexp.MustCompile(`\[.+?\]`)
func SquareBracketsLanguageParser(str string) []string { func SquareBracketsLanguageParser(str string) []string {
return SquareBracketsLanguageParserRegex.FindAllString(str, -1) return TrimPrefixSuffix(SquareBracketsLanguageParserRegex.FindAllString(str, -1), "[", "]")
} }

View File

@ -1,6 +1,9 @@
package parsers package parsers
import "strconv" import (
"strconv"
"strings"
)
func atoi(s string) (int, bool) { func atoi(s string) (int, bool) {
v, err := strconv.Atoi(s) v, err := strconv.Atoi(s)
@ -9,3 +12,11 @@ func atoi(s string) (int, bool) {
} }
return v, true 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
}