14-2 正则表达式

瘦欲@ 提交于 2020-01-26 05:33:29

https://city.zhenai.com/

打开网页,右键检查,在console中,$('#app>article>dl')可以得到css元素

package main

import (
    "regexp"
    "fmt"
)

const text = `My email is flliuqi@didiglobal.com
email is abc@def.org
email2 is kkk@qq.com
email3 is ddd@abc.com.cn`

func main() {
    //re := regexp.MustCompile(".+@.+\\..+")
    re := regexp.MustCompile(`([a-zA-Z0-9]+)@([a-zA-Z0-9.]+)(\.[a-zA-Z0-9]+)`)
    //match := re.FindString(text)
    match := re.FindAllString(text, -1)
    fmt.Println(match)
    match_sub := re.FindAllStringSubmatch(text, -1)
    fmt.Println(match_sub)
    for _, m  := range match_sub {
        fmt.Println(m)
    }
}
package main

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

func main() {
    resp, err := http.Get(
        "http://www.zhenai.com/zhenghun")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    if resp.StatusCode != http.StatusOK {
        fmt.Println("Error: status code", resp.StatusCode)
        return
    }
    // 1.utf8Reader := transform.NewReader(resp.Body,
    //         simplifiedchinese.GBK.NewDecoder())
    // 2.自动确认编码
    // e := determineEncoding(resp.Body)
    // utf8Reader := transform.NewReader(resp.Body,
    //         e.NewDecoder())
    all, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    //fmt.Printf("%s\n", all)
    printCityList(all)
}

func printCityList(contents []byte) {
    re := regexp.MustCompile(`<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)"[^>]*>([^<]+)</a>`)
    //matches := re.FindAll(contents, -1)
    //for _,m := range matches {
    //    fmt.Printf("%s\n", m)
    //}
    matches := re.FindAllSubmatch(contents, -1)
    for _, m := range matches {
//        for _, subMatch := range m {
//            fmt.Printf("%s", subMatch)
//        }
//        fmt.Println()
        fmt.Printf("City:%s, URL:%s\n", m[2], m[1])
    }
    fmt.Printf("Matches found:%d \n", len(matches))
}

//func determineEncoding(
//    r io.Reader) encoding.Encoding {
//    bytes, err := bufio.NewReader(r).Peek(1024)
//    if err != nil {
//        panic(err)
//    }
//    e, _, _ := charset.DetermineEncoding(
//        bytes, "")
//    return e
//}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!