如何在字符之间拆分字符串两

我想在两个字符之间拆分字符串( {{ 和 }})。

我有一个像这样的字符串{{number1}} + {{number2}} > {{number3}}

我正在寻找一些返回的东西:

[number1, number2, number3]


拉丁的传说
浏览 122回答 4
4回答

www说

你可以用正则表达式试试看:s := "{{number1}} + {{number2}} > {{number3}}"// Find all substrings in form {<var name>}re := regexp.MustCompile("{[a-z]*[0-9]*[a-z]*}")nums := re.FindAllString(s, -1)// Remove '{' and '}' from all substringsfor i, _ := range nums {&nbsp; &nbsp; nums[i] = strings.TrimPrefix(nums[i], "{")&nbsp; &nbsp; nums[i] = strings.TrimSuffix(nums[i], "}")}fmt.Println(nums)&nbsp; // output: [number1 number2 number3]您可以在此处尝试使用正则表达式:https://regex101.com/r/kkPWAS/1

慕雪6442864

您不需要使用库。您可以创建自己的函数。package mainconst r1 = '{'const r2 = '}'func GetStrings(in string) (out []string) {&nbsp; &nbsp; var tren string&nbsp; &nbsp; wr := false&nbsp; &nbsp; f := true&nbsp; &nbsp; for _, c := range in {&nbsp; &nbsp; &nbsp; &nbsp; if wr && c != r2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tren = tren + string(c)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if c == r1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f = !f&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wr = f&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if c == r2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wr = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if f {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = append(out, tren)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tren = ""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f = !f&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return}

波斯汪

使用模板解析器的困难方法^^package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings"&nbsp; &nbsp; "text/template/parse")func main() {&nbsp; &nbsp; input := "{{number1}} + {{number2}} > {{number3}}"&nbsp; &nbsp; out := parseit(input)&nbsp; &nbsp; fmt.Printf("%#v\n", out)}func parseit(input string) (out []string) {&nbsp; &nbsp; input = strings.Replace(input, "{{", "{{.", -1) // Force func calls to become variables.&nbsp; &nbsp; tree, err := parse.Parse("", input, "{{", "}}")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; visit(tree[""].Root, func(n parse.Node) bool {&nbsp; &nbsp; &nbsp; &nbsp; x, ok := n.(*parse.FieldNode)&nbsp; &nbsp; &nbsp; &nbsp; if ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = append(out, strings.Join(x.Ident, "."))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; })&nbsp; &nbsp; return}func visit(n parse.Node, fn func(parse.Node) bool) bool {&nbsp; &nbsp; if n == nil {&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }&nbsp; &nbsp; if !fn(n) {&nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; }&nbsp; &nbsp; if l, ok := n.(*parse.ListNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; for _, nn := range l.Nodes {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(nn, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if l, ok := n.(*parse.RangeNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; if !visit(l.BranchNode.Pipe, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if l.BranchNode.List != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(l.BranchNode.List, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if l.BranchNode.ElseList != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(l.BranchNode.ElseList, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if l, ok := n.(*parse.ActionNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; for _, c := range l.Pipe.Decl {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(c, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for _, c := range l.Pipe.Cmds {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(c, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if l, ok := n.(*parse.CommandNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; for _, a := range l.Args {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(a, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if l, ok := n.(*parse.PipeNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; for _, a := range l.Decl {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(a, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for _, a := range l.Cmds {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !visit(a, fn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}如果发生这种情况,您确实在操作模板字符串,但由于函数调用而未能执行此操作,并且您不想执行此操作input = strings.Replace(input, "{{", "{{.", -1) // Force func calls to become variables.您始终可以使用类似于var reMissingIdent = regexp.MustCompile(`template: :[0-9]+: function "([^"]+)" not defined`)func ParseTextTemplateAnyway(s string) (*texttemplate.Template, texttemplate.FuncMap, error) {&nbsp; &nbsp; fn := texttemplate.FuncMap{}&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; t, err := texttemplate.New("").Funcs(fn).Parse(s)&nbsp; &nbsp; &nbsp; &nbsp; if err == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return t, fn, err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; s := err.Error()&nbsp; &nbsp; &nbsp; &nbsp; res := reMissingIdent.FindAllStringSubmatch(s, -1)&nbsp; &nbsp; &nbsp; &nbsp; if len(res) > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fn[res[0][1]] = func(s ...interface{}) string { return "" }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return t, fn, err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // return nil, nil}

浮云间

使用正则表达式并将字符串的字母数字部分筛选为字符串数组。[A-Za-z]+[0-9]package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "regexp")func main() {&nbsp; &nbsp; s := `{{number1}} + {{number2}} > {{number3}}`&nbsp; &nbsp; re := regexp.MustCompile("[A-Za-z]+[0-9]")&nbsp; &nbsp; p := re.FindAllString(s, -1)&nbsp; &nbsp; fmt.Println(p) //[number1 number2 number3]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go