我從 HTML 檔案中輸入了這個字串:
<h1> Hello world </h1>
我想計算這個檔案的字數和字符數(不包括 HTML 元素)
例如:
Input
<h1>Hello</h1>\n<h1>Hello</h1>
Output
Characters : 10
Word : 2
我相信會有一個步驟我們首先決議這個 HTML 內容。但我不知道哪個包支持。
uj5u.com熱心網友回復:
您可以通過正則運算式找到它們。
input := []byte("<h1>Hello</h1>\n<h1>Hello</h1>")
tags, _ := regexp.Compile("(\\<\\/?[A-z0-9] \\>)|(\\\\[A-z]{1})")
// remove tags and backslash characters
input = tags.ReplaceAll(input, []byte(" "))
words, _ := regexp.Compile("[A-z0-9] ")
// find all matched words and count them
fmt.Println("total words: ", len(words.FindAll(input, -1)))
chars, _ := regexp.Compile("[A-z0-9]{1}")
// find all matched characters and count them
fmt.Println("total characters: ", len(chars.FindAll(input, -1)))
輸出:
total words: 2
total characters: 10
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/475735.html
標籤:去
