正则表达式

regexp, all below demos are here

package main

import (
    "regexp"
    "fmt"
)

const text = `
 my email is ccmouse@gmail.com
`
//func (re *Regexp) FindString(s string) string
func main(){
   re := regexp.MustCompile("ccmouse@gmail.com")  //MustCompile会认为你的正则没问题,所以不会跑出错误
   match :=re.FindString(text) 
   fmt.Println(match)
}
package main

import (
    "regexp"
    "fmt"
)

const text = `
 my email is ccmouse@gmail.com
`
//.表示任意字符, + 表示数量;
//要想匹配更精确,需用[0-9a-zA-Z]+
// \.会被go语言认出是转义字符,所以双引号内\\. 才表示.
// `` 就不会了,给出\. 就是. 
//如果要匹配点的话,``内用\. ; ""内用\\.
func main(){
   re := regexp.MustCompile(".+@.+\\..+")
   //等价 re := regexp.MustCompile(`.+@.+\..+`), 模版字符串里\.不会转义
   match :=re.FindString(text)
   fmt.Println(match) //my email is ccmouse@gmail.com
}
package main

import (
    "regexp"
    "fmt"
)

const text = `
 my email is ccmouse@gmail.com
 emial2 is simaon@sina.com
 emial3 is kkk@q.com
 emial4 is ddd@abc.com.cn
`
//.表示任意字符, + 表示数量; 
//如果要匹配点的话,``内用\. ; ""内用\\.
func main(){
   re := regexp.MustCompile(`[0-9a-zA-Z]+@[0-9a-zA-Z\.]+\.[a-zA-Z0-9]+`)  //@前面的空格不能匹配
   match :=re.FindAllString(text,-1)
   fmt.Println(match) 
   //[ccmouse@gmail.com simaon@sina.com kkk@q.com ddd@abc.com.cn]
}

提取字串

package main

import (
    "regexp"
    "fmt"
)

const text = `
 my email is ccmouse@gmail.com
 emial2 is simaon@sina.com
 emial3 is kkk@q.com
 emial4 is ddd@abc.com.cn
`
//.表示任意字符, + 表示数量; 
//如果要匹配点的话,``内用\. ; ""内用\\.
//.* 0个或多个;.+ 一个或多个; .* .+的范围都很大
func main(){
   re := regexp.MustCompile(`([0-9a-zA-Z]+)@([0-9a-zA-Z\.]+)\.([a-zA-Z0-9]+)`)  //@前面的空格不能匹配
   match :=re.FindAllStringSubmatch(text,-1)
   fmt.Println(match) 
   //[[ccmouse@gmail.com ccmouse gmail com] [simaon@sina.com simaon sina com] [kkk@q.com kkk q com] [ddd@abc.com.cn ddd abc.com cn]]
}

results matching ""

    No results matching ""