package test
import (
"fmt"
"regexp"
)
func RegixBase() {
//findTest()
//findIndexTest()
//findStringTest()
//findChinesString()
//findNumOrLowerLetter()
findAndReplace()
}
//Pass in []byte, return []byte
func findTest() {
str := "ab001234hah120210a880218end"
reg := ("\\d{6}") //Six consecutive numbers
("------Find------")
//Return the first string matching reg in str
data := ([]byte(str))
(string(data))
("------FindAll------")
//Return all strings matching reg in str
//The second parameter indicates the maximum number of returned numbers, and passing -1 indicates that all results are returned
dataSlice := ([]byte(str), -1)
for _, v := range dataSlice {
(string(v))
}
}
//Pass in []byte, return to the index of the first and last position
func findIndexTest() {
("------FindIndex------")
//Return the first and last positions of the first matching string
reg2 := ("start\\d*end") //start starts, ends ends, all numbers in the middle
str2 := "00start123endhahastart120PSend09start10000end"
//index[0] represents the start position, index[1] represents the end position
index := ([]byte(str2))
("start:", index[0], ",end:", index[1], str2[index[0]:index[1]])
("------FindAllIndex------")
//Return the first and last positions of all matching strings
indexSlice := ([]byte(str2), -1)
for _, v := range indexSlice {
("start:", v[0], ",end:", v[1], str2[v[0]:v[1]])
}
}
//Passing in string and returning string (more convenient)
func findStringTest() {
("------FindString------")
str := "ab001234hah120210a880218end"
reg := ("\\d{6}") //Six consecutive numbers
((str))
((str, -1))
//The following two methods are similar
((str))
(([]byte(str)))
}
//Find Chinese characters
func findChinesString() {
str := "Hello China and the world peace is good"
reg := ("[\\p{Han}]+")
((str, -1))
//[China World Peace is Good]
}
//Find numbers or lowercase letters
func findNumOrLowerLetter() {
str := "HAHA00azBAPabc09FGabHY99"
reg := ("[\\d|a-z]+")
((str, -1))
//[00az abc09 ab 99]
}
//Find and replace
func findAndReplace() {
str := "Welcome for Beijing-Tianjin CRH train."
reg := (" ")
((str, "@")) //Replace space with @ characters
//Welcome@for@Beijing-Tianjin@CRH@train.
}
import (
"fmt"
"regexp"
)
func RegixBase() {
//findTest()
//findIndexTest()
//findStringTest()
//findChinesString()
//findNumOrLowerLetter()
findAndReplace()
}
//Pass in []byte, return []byte
func findTest() {
str := "ab001234hah120210a880218end"
reg := ("\\d{6}") //Six consecutive numbers
("------Find------")
//Return the first string matching reg in str
data := ([]byte(str))
(string(data))
("------FindAll------")
//Return all strings matching reg in str
//The second parameter indicates the maximum number of returned numbers, and passing -1 indicates that all results are returned
dataSlice := ([]byte(str), -1)
for _, v := range dataSlice {
(string(v))
}
}
//Pass in []byte, return to the index of the first and last position
func findIndexTest() {
("------FindIndex------")
//Return the first and last positions of the first matching string
reg2 := ("start\\d*end") //start starts, ends ends, all numbers in the middle
str2 := "00start123endhahastart120PSend09start10000end"
//index[0] represents the start position, index[1] represents the end position
index := ([]byte(str2))
("start:", index[0], ",end:", index[1], str2[index[0]:index[1]])
("------FindAllIndex------")
//Return the first and last positions of all matching strings
indexSlice := ([]byte(str2), -1)
for _, v := range indexSlice {
("start:", v[0], ",end:", v[1], str2[v[0]:v[1]])
}
}
//Passing in string and returning string (more convenient)
func findStringTest() {
("------FindString------")
str := "ab001234hah120210a880218end"
reg := ("\\d{6}") //Six consecutive numbers
((str))
((str, -1))
//The following two methods are similar
((str))
(([]byte(str)))
}
//Find Chinese characters
func findChinesString() {
str := "Hello China and the world peace is good"
reg := ("[\\p{Han}]+")
((str, -1))
//[China World Peace is Good]
}
//Find numbers or lowercase letters
func findNumOrLowerLetter() {
str := "HAHA00azBAPabc09FGabHY99"
reg := ("[\\d|a-z]+")
((str, -1))
//[00az abc09 ab 99]
}
//Find and replace
func findAndReplace() {
str := "Welcome for Beijing-Tianjin CRH train."
reg := (" ")
((str, "@")) //Replace space with @ characters
//Welcome@for@Beijing-Tianjin@CRH@train.
}