1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Go语言使用正则表达式提取网页文本

Go语言使用正则表达式提取网页文本

时间:2023-01-31 21:10:15

相关推荐

Go语言使用正则表达式提取网页文本

后端开发|Golang

Go语言,正则

后端开发-Golang

爱卡发卡平台源码,keil配置vscode,ubuntu 手写笔,tomcat替换产品,sqlite shm文件,服务器3m独享,canvas拼图js插件,java 前端ext 框架,爬虫目的,宇润php,seo优化流程图,房产中介 网站模板,网页设计案例源代码,怎么修改php模板,asp批量生成html静态页面方法,图书管理系统课程设计软件,lms算法matlab程序lzw

示例:在字符串 1000abcd123 中找出前后两个数字。

手机app软件模板源码,mac系统和ubuntu,tomcat日志一半放哪,python爬虫在线学费,南宁php工程师招聘网,seo难学排名lzw

例子1:匹配到这个字符串的例子

单城市手机版仿58同城php源码,锐角云安装ubuntu,反爬虫重定向,php 137,seo济南招聘lzw

package main import( "fmt" "regexp") var digitsRegexp = regexp.MustCompile(`(\d+)\D+(\d+)`) func main(){ someString:="1000abcd123" fmt.Println(digitsRegexp.FindStringSubmatch(someString))}

上面代码输出:

[1000abcd123 1000 123]

例子2:使用带命名的正则表达式

package main import( "fmt" "regexp") var myExp=regexp.MustCompile(`(?P\d+)\.(\d+).(?P\d+)`) func main(){ fmt.Printf("%+v",myExp.FindStringSubmatch("1234.5678.9"))}

上面代码输出,所有匹配到的都输出了:

[1234.5678.9 1234 5678 9]

这里的Named capturing groups (?P) 方式命名正则表达式是 python、Go语言特有的, java、c# 是 (?) 命名方式。

例子3:对正则表达式类扩展一个获得所有命名信息的方法,并使用它。

package main import( "fmt" "regexp") //embed regexp.Regexp in a new type so we can extend ittype myRegexp struct{*regexp.Regexp} //add a new method to our new regular expression typefunc(r *myRegexp)FindStringSubmatchMap(s string) map[string]string{ captures:=make(map[string]string)match:=r.FindStringSubmatch(s) if match==nil{ return captures }for i,name:=range r.SubexpNames(){ //Ignore the whole regexp match and unnamed groups if i==0||name==""{ continue } captures[name]=match[i]} return captures} //an example regular expressionvar myExp=myRegexp{regexp.MustCompile(`(?P\d+)\.(\d+).(?P\d+)`)} func main(){ mmap:=myExp.FindStringSubmatchMap("1234.5678.9") ww:=mmap["first"] fmt.Println(mmap) fmt.Println(ww)}

上面代码的输出结果:

map[first:1234 second:9]
1234

例子4,抓取限号信息,并记录到一个Map中。

package main import( "fmt" iconv "/djimenez/iconv-go" "io/ioutil" "net/http" "os" "regexp") // embed regexp.Regexp in a new type so we can extend ittype myRegexp struct{ *regexp.Regexp} // add a new method to our new regular expression typefunc(r *myRegexp)FindStringSubmatchMap(s string)[](map[string]string){ captures:=make([](map[string]string),0)matches:=r.FindAllStringSubmatch(s,-1)if matches==nil{ return captures }names:=r.SubexpNames()for _,match:=range matches{ cmap:=make(map[string]string) for pos,val:=range match{ name:=names[pos] if name==""{continue } /*fmt.Println("+++++++++")fmt.Println(name)fmt.Println(val) */ cmap[name]=val } captures=append(captures,cmap)}return captures} // 抓取限号信息的正则表达式var myExp=myRegexp{regexp.MustCompile(`自(?P[\d]{4})年(?P[\d]{1,2})月(?P[\d]{1,2})日至(?P[\d]{4})年(?P[\d]{1,2})月(?P[\d]{1,2})日,星期一至星期五限行机动车车牌尾号分别为:(?P[\d])和(?P[\d])、(?P[\d])和(?P[\d])、(?P[\d])和(?P[\d])、(?P[\d])和(?P[\d])、(?P[\d])和(?P[\d])`)} func ErrorAndExit(err error){ fmt.Fprintln(os.Stderr,err) os.Exit(1)} func main(){ response,err:=http.Get("http://www./zhuanti/10weihao/index.html") defer response.Body.Close()if err!=nil{ ErrorAndExit(err) }input,err:=ioutil.ReadAll(response.Body) if err!=nil{ ErrorAndExit(err) } body :=make([]byte,len(input)) iconv.Convert(input,body,"gb2312","utf-8")mmap:=myExp.FindStringSubmatchMap(string(body))fmt.Println(mmap)}

上述代码输出:

[map[n32:0 n22:9 emonth:7 n11:3 n41:1 n21:4 n52:7 bmonth:4 n51:2 bday:9 n42:6 byear: eday:7 eyear: n12:8 n31:5]map[emonth:10 n41:5 n52:6 n31:4 byear: n51:1 eyear: n32:9 bmonth:7 n22:8 bday:8 n11:2 eday:6 n42:0 n21:3 n12:7]map[bday:7 n51:5 n22:7 n31:3 eday:5 n32:8 byear: bmonth:10 emonth:1 eyear: n11:1 n12:6 n52:0 n21:2 n42:9 n41:4]map[eyear: byear: n22:6 eday:10 bmonth:1 n41:3 n32:7 n31:2 n21:1 n11:5 bday:6 n12:0 n51:4 n42:8 emonth:4 n52:9]]

更多go语言知识请关注PHP中文网go语言教学栏目。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。