zoukankan      html  css  js  c++  java
  • [GO]正则表达式

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        s := "abc azc a7c aac 888 a9c tac"
        reg1 := regexp.MustCompile(`a.c`) //创建解释规则,它会解析正则表达式,如果成功则返回解析器,注意这里写的是反引号
        if reg1 == nil{ //解析失败,返回nil
            fmt.Println("regexp err1")
            return
        }
        result1 := reg1.FindAllStringSubmatch(s, -1)
        fmt.Println("result = ", result1)
    }

    对于字符串的操作的话最好使用strings这个包进行,strings不行的时候再使用正则表达式的包

    而且strings这个包在字符串的处理上效率也高于正则

    FindAllStringSubmatch(s, -1)里,这个-1表壳输出所有的匹配结果,也可以写成正数1,2表示 输入1个或者2个匹配的结果

    小例子

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        buf := "3.14 566 adfa 1.23 7. 8.00 lasdfjej 6.66"
        reg := regexp.MustCompile(`d.d+`)//+表示前一个条件的字符一次或多次,反引号代表的原生字符串
        if reg == nil{
            fmt.Println("muctcomplite error")
            return
        }
        result := reg.FindAllStringSubmatch(buf, -1)
        fmt.Println("result = ", result)
    
        //试着写一个调取网面指定内容的正则
        buf = `
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <title>Go语言标准库文档中文版 | Go语言中文网 | Golang中文社区 | Golang中国</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <meta charset="utf-8">
        <link rel="shortcut icon" href="/static/img/go.ico">
        <link rel="apple-touch-icon" type="image/png" href="/static/img/logo2.png">
        <meta name="author" content="polaris <polaris@studygolang.com>">
        <meta name="keywords" content="中文, 文档, 标准库, Go语言,Golang,Go社区,Go中文社区,Golang中文社区,Go语言社区,Go语言学习,学习Go语言,Go语言学习园地,Golang 中国,Golang中国,Golang China, Go语言论坛, Go语言中文网">
        <meta name="description" content="Go语言文档中文版,Go语言中文网,中国 Golang 社区,Go语言学习园地,致力于构建完善的 Golang 中文社区,Go语言爱好者的学习家园。分享 Go 语言知识,交流使用经验">
    </head>
        <div>哈哈</div>
        <div>你过来啊</div>
        <div>这是一个测试</div>
    <frameset cols="15,85">
        <frame src="/static/pkgdoc/i.html">
        <frame name="main" src="/static/pkgdoc/main.html" tppabs="main.html" >
        <noframes>
        </noframes>
    </frameset>
    </html>
    `
        reg = regexp.MustCompile(`<div>(.*)</div>`)
        if reg == nil{
            fmt.Println("muctcomplite error")
            return
        }
        result = reg.FindAllStringSubmatch(buf, -1)
        for _, text := range result{
            fmt.Println("test[1] = ", text[1])
        }
    }

    执行结果为

    result =  [[3.14] [1.23] [8.00] [6.66]]
    test[1] =  哈哈
    test[1] =  你过来啊
    test[1] =  这是一个测试
  • 相关阅读:
    CodeForces 734F Anton and School
    CodeForces 733F Drivers Dissatisfaction
    CodeForces 733C Epidemic in Monstropolis
    ZOJ 3498 Javabeans
    ZOJ 3497 Mistwald
    ZOJ 3495 Lego Bricks
    CodeForces 732F Tourist Reform
    CodeForces 732E Sockets
    CodeForces 731E Funny Game
    CodeForces 731D 80-th Level Archeology
  • 原文地址:https://www.cnblogs.com/baylorqu/p/9662166.html
Copyright © 2011-2022 走看看