go语言之正则表达式
go语言之正则表达式
//regexp
package main
import (
"fmt"
"regexp"
)
func main() {
//需要匹配的模式
//reg := regexp.MustCompile("\\w+") 正则表达式中的\需要转义
reg := regexp.MustCompile(`^z.*1$`)
//返回匹配到的结果
result := reg.FindAllString("zhangsan1", -1)
fmt.Printf("%v\n", result)
//演示一个匹配不到的例子
reg1 := regexp.MustCompile(`^z(.*)1$`)
result1 := reg1.FindAllString("zhangsand", -1)
fmt.Printf("%v\n", result1)
reg2 := regexp.MustCompile(`^z(.{1})(.{1})(.*)1$`)
result2 := reg2.FindAllString("zhangsan1", -1)
fmt.Printf("%v\n", result2)
}
// simple_crawler
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func main() {
url := "https://movie.douban.com/subject/24751763/"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
sHtml, _ := ioutil.ReadAll(resp.Body)
reg := regexp.MustCompile(`(.*)`)
result := reg.FindAllStringSubmatch(string(sHtml), -1)
fmt.Println(result[0][1])
reg1 := regexp.MustCompile(`(.*)`)
result1 := reg1.FindAllStringSubmatch(string(sHtml), -1)
fmt.Println(result1[0][1])
}
网站名称:go语言之正则表达式
标题链接:http://pwwzsj.com/article/jgcgps.html