goland 正则爬取SFZ号码

180it 2021-10-03 AM 1806℃ 0条
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "regexp"
)

var (
    //3-30625-1970-04-26-0474
    //reLink = `[1-6]\d{5}-(19\d{2})|(20(0\d)|(1[0-8]))-(0[1-9])|(1[012])-((0[1-9])|([12]\d)|(3[01]))-\d{3}[\dx]` //需要根据实际情况书写规则
    reLink = `[1-6]\d{5}((19\d{2})|(20((0\d)|(1[0-8]))))(0[1-9])|(1[012])((0[1-9])|([12]\d)|(3[01]))\d{3}[\dx]` //需要根据实际情况书写规则

)

func HandleError(err error, when string) {
    if err != nil {
        fmt.Println(when, err)
        os.Exit(1)
    }
}

func main() {

    html := GetHtml("http://www.shaoxing.com.cn/p/2771751.html")

    re := regexp.MustCompile(reLink)
    AallString := re.FindAllStringSubmatch(html, -1)
    for _, x := range AallString {
        fmt.Println(x[0])
    }

}

func GetHtml(url string) string {
    resp, err := http.Get(url)
    HandleError(err, "http.Get")
    bytes, _ := ioutil.ReadAll(resp.Body)
    html := string(bytes)
    return html
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

goland 正则爬取SFZ号码