Skip to content

Instantly share code, notes, and snippets.

@s-hiiragi
Last active February 6, 2017 12:21
Show Gist options
  • Save s-hiiragi/5b53da3110c52ade3201904799a62f1a to your computer and use it in GitHub Desktop.
Save s-hiiragi/5b53da3110c52ade3201904799a62f1a to your computer and use it in GitHub Desktop.
Goで正規表現にマッチする文字列を取り出すサンプル
package main
import (
"fmt"
"regexp"
)
func main() {
fmt.Println("Hello, playground")
s := "hoge C:¥hoge¥fuga¥piyo.c 1000"
r := regexp.MustCompile(`^([_0-9a-zA-Z]+)\s+(\S+)\s+(\d+)`)
m := r.FindStringSubmatch(s)
if m == nil {
fmt.Println("no match")
return
}
fmt.Println(m[0]) // ==> hoge C:¥hoge¥fuga.piyo.c 1000
fmt.Println(m[1]) // ==> hoge
fmt.Println(m[2]) // ==> C:¥hoge¥fuga¥piyo.c
fmt.Println(m[3]) // ==> 1000
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment