Erlo

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

2021-04-25 20:01:34 发布   716 浏览  
页面报错/反馈
收藏 点赞

     对于正则表达式的语法,虽然曾经有熟悉过,但只会用比较简单的匹配模式,现在长时间不用,连简单的匹配写起来都有点成为硬伤的赶脚,不过这里终点不是正在表达式的学习了,咱来看看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
	}
        src := string(body)
               //获取百度返回给我们的body内容,转换成字符串用于下面正则表达式的操作:

              

        // 去除STYLE
	re, _ = regexp.Compile("\
       关于正则表达式的入门就这么些吧,水平有限,也就只能分享到这了。。。。





登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认