Go Regular expression

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go Regular Expression Daquan

Ackage Test

Import (
"FMT"
"RegExp"
)

Func regixbase () {
Findtest ()
Findindextest ()
Findstringtest ()
Findchinesstring ()
Findnumorlowerletter ()
Findandreplace ()
}

Incoming []byte, return []byte
Func findtest () {
str: = "Ab001234hah120210a880218end"
Reg: = RegExp. Mustcompile ("\d{6}")//six-bit sequential number
Fmt. Println ("--find--")
Returns the first string in str that matches a reg
Data: = Reg. Find ([]byte (str))
Fmt. Println (String (data))

fmt.Println("------FindAll------")  //返回str中所有匹配reg的字符串  //第二个参数表示最多返回的个数,传-1表示返回所有结果  dataSlice := reg.FindAll([]byte(str), -1)  for _, v := range dataSlice {      fmt.Println(string(v))  }  

}

Incoming []byte, return to the first-last position index
Func findindextest () {
Fmt. Println ("--findindex--")
Returns the first end of a matched string
REG2: = RegExp. Mustcompile ("Start\d*end")//start start, end ends, all numbers in the middle
STR2: = "00start123endhahastart120psend09start10000end"
Index[0] Indicates the start position, index[1] indicates the end position
Index: = reg2. FindIndex ([]byte (STR2))
Fmt. Println ("Start:", Index[0], ", End:", index[1], str2[index[0]:index[1]])

fmt.Println("------FindAllIndex------")  //返回所有匹配的字符串首末位置  indexSlice := reg2.FindAllIndex([]byte(str2), -1)  for _, v := range indexSlice {      fmt.Println("start:", v[0], ",end:", v[1], str2[v[0]:v[1]])  }  

}

String passed in, returns string (more convenient)
Func findstringtest () {
Fmt. Println ("--findstring--")

str := "ab001234hah120210a880218end"  reg := regexp.MustCompile("\\d{6}") //六位连续的数字  fmt.Println(reg.FindString(str))  fmt.Println(reg.FindAllString(str, -1))  //以下两个方法是类似的  fmt.Println(reg.FindStringIndex(str))  fmt.Println(reg.FindIndex([]byte(str)))  

}

Find Chinese characters
Func findchinesstring () {
str: = "Hello china Hello World Peace Hi Good"
Reg: = RegExp. Mustcompile ("[\p{han}]+")
Fmt. Println (Reg. Findallstring (str,-1))

//[中国 世界和平 好]  

}

Find numbers or lowercase letters
Func Findnumorlowerletter () {
str: = "Haha00azbapabc09fgabhy99"
Reg: = RegExp. Mustcompile ("[\d|a-z]+")
Fmt. Println (Reg. Findallstring (str,-1))

//[00az abc09 ab 99]  

}

Find and replace
Func Findandreplace () {
str: = "Welcome for Beijing-tianjin CRH train."
Reg: = RegExp. Mustcompile ("")
Fmt. Println (Reg. Replaceallstring (str, "@"))//Replace the space with the @ character

//Welcome@for@Beijing-Tianjin@CRH@train.  

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.