Go Regular expression

Source: Internet
Author: User
Go Regular Expression Encyclopedia

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 consecutive digits
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------")  
//returns the string//second parameter of all matching Reg in Str that  
represents the maximum number returned, and 1 indicates that all results are returned  
dataslice: = Reg. FindAll ([]byte (str),-1)  
for _, V: = range Dataslice {  
    fmt. Println (String (v))  
}  

}

Pass in []byte, return to index at first end position
Func findindextest () {
Fmt. Println ("--findindex--")
Returns the first position of a matching string
REG2: = RegExp. Mustcompile ("Start\d*end")//start start, end ends, middle is full of numbers
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------")  
//returns the top position of all matching strings  
Indexslice: = reg2. Findallindex ([]byte (STR2),-1)  
for _, V: = range Indexslice {  
    fmt. Println ("Start:", V[0], ", End:", v[1], str2[v[0]:v[1])  
}  

}

Pass in string, return string (more convenient)
Func findstringtest () {
Fmt. Println ("--findstring--")

str: = "Ab001234hah120210a880218end"  
reg: = RegExp. Mustcompile ("\\d{6}")//six-bit sequential number  
FMT. Println (Reg. FindString (str))  
FMT. Println (Reg. Findallstring (str,-1))  
//The following two methods are similar to  
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))

[China's world peace and good]  

}

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 space with @ 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.