【发布时间】:2014-01-26 19:50:21
【问题描述】:
从 Golang 中的字符串中提取内部子字符串的最佳方法是什么?
输入:
"Hello <p> this is paragraph </p> this is junk <p> this is paragraph 2 </p> this is junk 2"
输出:
"this is paragraph \n
this is paragraph 2"
是否有任何 Go 的字符串包/库已经做了类似的事情?
package main
import (
"fmt"
"strings"
)
func main() {
longString := "Hello world <p> this is paragraph </p> this is junk <p> this is paragraph 2 </p> this is junk 2"
newString := getInnerStrings("<p>", "</p>", longString)
fmt.Println(newString)
//output: this is paragraph \n
// this is paragraph 2
}
func getInnerStrings(start, end, str string) string {
//Brain Freeze
//Regex?
//Bytes Loop?
}
谢谢
【问题讨论】:
-
Here。阅读关于子匹配的部分;它应该对你有帮助。
-
是的,我看到了,但我不确定或不确定这是否是正确的方法。已添加书签以供将来参考。
标签: regex string go byte substring