Jquery中文网 www.jquerycn.cn
Jquery中文网 >  后端编程  >  Go语言  >  正文 go编程之简单正则表达式使用

go编程之简单正则表达式使用

发布时间:2021-04-25   编辑:www.jquerycn.cn
jquery中文网为您提供go编程之简单正则表达式使用等资源,欢迎您收藏本站,我们将为您提供最新的go编程之简单正则表达式使用资源

     对于正则表达式的语法,虽然曾经有熟悉过,但只会用比较简单的匹配模式,现在长时间不用,连简单的匹配写起来都有点成为硬伤的赶脚,不过这里终点不是正在表达式的学习了,咱来看看go语言对于正则表达式的支持及简单的使用就好,程序主要参考go web一书所写,大家可以一起来学习。

    主要使用的包为regexp,使用的时候import "regexp“ 就行啦,以下是常用的几个接口     

     func Match( pattern string, b [ ] byte) ( matched bool, error error)
     func MatchReader( pattern string, r io. RuneReader) ( matched bool, error error)
     func MatchString( pattern string, s string) ( matched bool, error error)

     分别用来匹配slice切片、string等数据类型,返回值为:匹配上返回true,匹配补上返回false,异常err为nil

      

     下面代码为go用来匹配简单的11的电话号码:

      

func IsTelNumber(telNum string) (bool, error) {
	m, err := regexp.MatchString("^[0-9]{11}$", ip)
	if m {
		return true, err
	} else {
		return false, err
	}
}

        fmt.Println("test go regexp (telNum)")
	retVal, _ := IsIPAdd("15202992212")
	if retVal {
		fmt.Println("this is a telphone address")
	} else {
		fmt.Println("this is not a telphone address")
	}


     至于比较高难度的应用的首要条件是你得写出好的正则表达式,其次在正则表达式除了用来匹配我们需要的数据外,还可以获取这些数据、修改这些数据等,我们的regexp包也给我们提供了这些方法来完成任何我们想要完成的工作,主要方法有:

     func Compile( expr string) ( * Regexp, error)
     func CompilePOSI X( expr string) ( * Regexp, error)
     func MustCompile( str string) * Regexp
     func MustCompilePOSI X( str string) * Regexp

     以上四位用来判断我们的正则表达式是否合法,合法的话会返回Regexp对象吧,可以这么叫呗

    func ( re * Regexp) Find( b [ ] byte) [ ] byte
    func ( re * Regexp) FindAll( b [ ] byte, n int) [ ] [ ] byte
    func ( re * Regexp) FindAllI ndex( b [ ] byte, n int) [ ] [ ] int
    func ( re * Regexp) FindAllString( s string, n int) [ ] string
    func ( re * Regexp) FindAllStringI ndex( s string, n int) [ ] [ ] int
    func ( re * Regexp) FindAllStringSubmatch( s string, n int) [ ] [ ] string
    func ( re * Regexp) FindAllStringSubmatchI ndex( s string, n int) [ ] [ ] int
    func ( re * Regexp) FindAllSubmatch( b [ ] byte, n int) [ ] [ ] [ ] byte
    func ( re * Regexp) FindAllSubmatchI ndex( b [ ] byte, n int) [ ] [ ] int
    func ( re * Regexp) FindI ndex( b [ ] byte) ( loc [ ] int)
    func ( re * Regexp) FindReaderI ndex( r io. RuneReader) ( loc [ ] int)
    func ( re * Regexp) FindReaderSubmatchI ndex( r io. RuneReader) [ ] int
    func ( re * Regexp) FindString( s string) string
    func ( re * Regexp) FindStringI ndex( s string) ( loc [ ] int)
    func ( re * Regexp) FindStringSubmatch( s string) [ ] string
    func ( re * Regexp) FindStringSubmatchI ndex( s string) [ ] int
    func ( re * Regexp) FindSubmatch( b [ ] byte) [ ] [ ] byte 
    func ( re * Regexp) FindSubmatchI ndex( b [ ] byte) [ ] int

    以上这一长串用来对我们符合正则表达式规则的字符串、slice等的处理,至于具体的功能大家可以网上查一查,就不详解了,下面附上一个简单的处理字符串的例子:


   

        resp, err := http.Get("http://www.baidu.com")
	if err != nil {
		fmt.Println("get baidu respose error")
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("http read error")
		return
	}
        <span style="font-family: Arial, Helvetica, sans-serif;">src</span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(192, 192, 192);"> </span><span style="font-family: Arial, Helvetica, sans-serif;">:=</span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(192, 192, 192);"> </span><span style="font-family: Arial, Helvetica, sans-serif; color: rgb(0, 0, 128);">string</span><span style="font-family: Arial, Helvetica, sans-serif;">(body)</span>
               //获取百度返回给我们的body内容,转换成字符串用于下面正则表达式的操作:

              

        // 去除STYLE
	re, _ = regexp.Compile("\\<style[\\S\\s]  ?\\</style\\>")
	src = re.ReplaceAllString(src, " ")
	// 去除SCRI PT
	re, _ = regexp.Compile("\\<script[\\S\\s]  ?\\</script\\>")
	src = re.ReplaceAllString(src, " ")
	// 去除所有尖括号内 的HTML代码, 并换成换行符
	re, _ = regexp.Compile("\\<[\\S\\s]  ?\\>")
	src = re.ReplaceAllString(src, "\n")
       关于正则表达式的入门就这么些吧,水平有限,也就只能分享到这了。。。。





到此这篇关于“go编程之简单正则表达式使用”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
go编程之简单正则表达式使用
Go 语言结构
从零开始学习GO语言-搭建Go语言开发环境-快速开发入门第一个小程序
Go 语言 if 语句
正则表达式使用详解
Go 语言学习第一章节
想系统学习GO语言(Golang
正则表达式在网络编程中的运用
go语言和python哪个难
Golang中defer关键字实现原理

[关闭]