侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Go  /  管理员 发布于 5年前   331

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

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

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<first>\d+)\.(\d+).(?P<second>\d+)`) func main(){    fmt.Printf("%+v",myExp.FindStringSubmatch("1234.5678.9"))}

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

[1234.5678.9 1234 5678 9]

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

例子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<first>\d+)\.(\d+).(?P<second>\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 "github.com/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<byear>[\d]{4})年(?P<bmonth>[\d]{1,2})月(?P<bday>[\d]{1,2})日至(?P<eyear>[\d]{4})年(?P<emonth>[\d]{1,2})月(?P<eday>[\d]{1,2})日,星期一至星期五限行机动车车牌尾号分别为:(?P<n11>[\d])和(?P<n12>[\d])、(?P<n21>[\d])和(?P<n22>[\d])、(?P<n31>[\d])和(?P<n32>[\d])、(?P<n41>[\d])和(?P<n42>[\d])、(?P<n51>[\d])和(?P<n52>[\d])`)} func ErrorAndExit(err error){    fmt.Fprintln(os.Stderr,err)    os.Exit(1)} func main(){    response,err:=http.Get("http://www.bjjtgl.gov.cn/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:2012 eday:7 eyear:2012 n12:8 n31:5]map[emonth:10 n41:5 n52:6 n31:4 byear:2012 n51:1 eyear:2012 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:2012 bmonth:10 emonth:1 eyear:2013 n11:1 n12:6 n52:0 n21:2 n42:9 n41:4]map[eyear:2013 byear:2013 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语言知识请关注go语言教程栏目。

以上就是Go语言使用正则表达式提取网页文本的详细内容,更多请关注其它相关文章!


  • 上一条:
    Go Modules使用详解
    下一条:
    从go语言闭包谈函数式编程
  • 昵称:

    邮箱:

    1条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-09
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-12
    • 2021-01
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-08
    • 2024-11
    • 2025-02
    • 2025-04
    • 2025-05
    • 2025-06
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客