Go regular expression matches string, replaces string
Regular expressions
package main import ( "fmt" "regexp" ) func main() { match, err := ("h[a-z]+.*d$", "hello world") if err != nil { panic(err) } (match) match, err = ("h[a-z]+.*d$", "ello world") if err != nil { panic(err) } (match) } // $ go run // The output is as follows/** true false */
Match all substrings
package main import ( "fmt" "regexp" ) func main() { c, err := ("h[a-z]") if err != nil { panic(err) } res := ("hello world", -1) ("res = %v\n", res) res2 := ("hello world hi ha h1", -1) ("res2 = %v\n", res2) } // $ go run // The output is as follows/** res = [he] res2 = [he hi ha] */
Replace all substrings
package main import ( "fmt" "regexp" ) func main() { c, err := ("h[a-z]") if err != nil { panic(err) } res := ([]byte("hello world"), []byte("?")) ("res = %s\n", res) res2 := ([]byte("hello world hi ha h1"), []byte("?")) ("res2 = %s\n", res2) } // $ go run // The output is as follows/** res = ?llo world res2 = ?llo world ? ? h1 */
Match Chinese
package main import ( "fmt" "regexp" ) func main() { match, err := ("\\x{4e00}-\\x{9fa5}", "hello world") if err != nil { panic(err) } (match) match, err = ("\\p{Han}+", "hello world") if err != nil { panic(err) } (match) } // $ go run // The output is as follows/** false true */
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.