package util import ( "github.com/mizuki1412/go-core-kit/class/exception" "golang.org/x/net/html" "regexp" "strings" ) func RegexpFindFirst(content, regexpExpress string) string { //`"remandCode":"([^"]+)"` //`"installNum":(\d+(\.\d+)?)` // 定义正则表达式匹配 re := regexp.MustCompile(regexpExpress) // 在内容中查找匹配的字符串 match := re.FindStringSubmatch(content) if len(match) < 2 { panic(exception.New("regexp not found")) } // 第一个捕获组中的值即为要查找的 ok := match[1] return ok } func HTMLContentFindFirst(content, attrVal string) string { // 使用 html.Parse 解析 HTML 内容 doc, err := html.Parse(strings.NewReader(content)) if err != nil { panic(exception.New(err.Error())) } // 递归函数来查找节点 var res string var findProjectName func(*html.Node) findProjectName = func(n *html.Node) { // 查找标签为 input 的节点 if n.Type == html.ElementNode && n.Data == "input" { for _, attr := range n.Attr { if attr.Key == "name" && attr.Val == attrVal { // 获取 value 属性的值作为 res for _, attr := range n.Attr { if attr.Key == "value" { res = attr.Val return } } } } } // 递归处理子节点 for c := n.FirstChild; c != nil; c = c.NextSibling { findProjectName(c) } } findProjectName(doc) return res }