Regular Expression in Go
The regular expression in Go is well-documented and close to other popular languages such as Python. All the functionalities are supported by a built-in package called regexp
. Like Python, you need to compile the regular expression before you can use it. The styntax is specified in this document [1]. Here is an example using regex match [2].
// go run regex.go
package main
import "fmt"
import "regexp"
func main() {
var r, _ = regexp.Compile("C.*")
var language_list = [...]string{"C", "C++", "C#", "Go", "C#", "Java"}
for _, lan := range language_list{
if r.MatchString(lan){
fmt.Printf("%s matches C.*\n", lan)
}else{
fmt.Printf("%s doesn't mathch C.*\n", lan)
}
}
}
You should see the following result
C matches C.*
C++ matches C.*
C# matches C.*
Go doesn't mathch C.*
C# matches C.*
Java doesn't mathch C.*