regex - 在 Golang 中从 HTML 中提取文本内容

标签 regex string go byte substring

在 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?
}

谢谢

最佳答案

Don't use regular expressions尝试解释 HTML。使用fully capable HTML tokenizer and parser .

我建议您阅读 this article关于 CodingHorror。

关于regex - 在 Golang 中从 HTML 中提取文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000277/

相关文章:

php - 带有单词边界的特定数字或字符后的省略号

json - 将复杂的 JSON 转换为 Golang 中的 map

Golang 上的 JSON - 解码 Graphite 数据

javascript - 如何使用 JavaScript 的 RegExp 执行条件替换?

javascript - javascript Replace() 遇到问题

php - 正则表达式来检测写成单词的数字

go - 将状态存储在golang二进制文件中

c++ - str.clear() 和 str = ""之间的区别

Java-在数组中存储字符串的字符

c++ - 命名空间 std 中的字符串没有命名类型