使用 golang 和 regexp 摆脱外部标签?

get rid of the outer tag using golang and regexp?

我正在使用 goalng 做一些模板,想去掉外部标签。 如下:

  input := `aaa<div><dxh r="4" spans="1:15"><c r="A4" s="7"><v>{{4567}}

</v></c><c r="B4" t="s" s="7"><v>11</v></c><c r="C4" t="s" s="7"><v>12</v>

</c><c r="M4" t="s" s="8"><v>20</v></c></dxh>aaa</div>bbb<dxh>{{12345}}

</dxh>amrambler`

我想获取字符串。它省略了标签 "<dxh ....>","</dxh>" 。只保留它们之间的内容,"{{4567}}""{{12345}}"

str=`aaa<div>{{4567}}aaa</div>bbb{{12345}}amrambler`

提前致谢!

您可以使用以下方法获得所需的输出。

package main

import (
   "fmt"
   "regexp"
)

func main() {
    re := regexp.MustCompile("(?s)<dxh[^>]*>.*?({{[^}]*}}).*?</dxh>")

    input := `aaa<div><dxh r="4" spans="1:15"><c r="A4" s="7"><v>{{4567}}
              </v></c><c r="B4" t="s" s="7"><v>11</v></c><c r="C4" t="s" s="7"><v>12</v>
              </c><c r="M4" t="s" s="8"><v>20</v></c></dxh>aaa</div>bbb<dxh>{{12345}}
              </dxh>amrambler`

    res := re.ReplaceAllString(input, "")
    fmt.Println(res) // aaa<div>{{4567}}aaa</div>bbb{{12345}}amrambler  
}

GoPlay