【问题标题】:Extract text content from HTML in Golang在 Golang 中从 HTML 中提取文本内容
【发布时间】: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


【解决方案1】:

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

我建议你阅读 CodingHorror 上的 this article

【讨论】:

【解决方案2】:

这是我经常使用的函数。

func GetInnerSubstring(str string, prefix string, suffix string) string {
    var beginIndex, endIndex int
    beginIndex = strings.Index(str, prefix)
    if beginIndex == -1 {
        beginIndex = 0
        endIndex = 0
    } else if len(prefix) == 0 {
        beginIndex = 0
        endIndex = strings.Index(str, suffix)
        if endIndex == -1 || len(suffix) == 0 {
            endIndex = len(str)
        }
    } else {
        beginIndex += len(prefix)
        endIndex = strings.Index(str[beginIndex:], suffix)
        if endIndex == -1 {
            if strings.Index(str, suffix) < beginIndex {
                endIndex = beginIndex
            } else {
                endIndex = len(str)
            }
        } else {
            if len(suffix) == 0 {
                endIndex = len(str)
            } else {
                endIndex += beginIndex
            }
        }
    }

    return str[beginIndex:endIndex]
}

你可以在操场上试试,https://play.golang.org/p/Xo0SJu0Vq4

【讨论】:

    【解决方案3】:

    StrExtract 检索两个分隔符之间的字符串。

    StrExtract(sExper, cAdelim, cCdelim, nOccur)

    sExper:指定要搜索的表达式。 sAdelim:指定 分隔 sExper 开头的字符。

    sCdelim:指定分隔 sExper 结尾的字符。

    nOccur:指定在 sExper 中的哪个 cAdelim 出现处开始 提取。

    Go Play

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        s := "a11ba22ba333ba4444ba55555ba666666b"
        fmt.Println("StrExtract1: ", StrExtract(s, "a", "b", 5))
    }
    
    func StrExtract(sExper, sAdelim, sCdelim string, nOccur int) string {
    
        aExper := strings.Split(sExper, sAdelim)
    
        if len(aExper) <= nOccur {
            return ""
        }
    
        sMember := aExper[nOccur]
        aExper = strings.Split(sMember, sCdelim)
    
        if len(aExper) == 1 {
            return ""
        }
    
        return aExper[0]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 2013-02-05
      • 2016-09-09
      • 2021-09-09
      • 2012-09-03
      相关资源
      最近更新 更多