Last active
February 6, 2017 12:21
-
-
Save s-hiiragi/5b53da3110c52ade3201904799a62f1a to your computer and use it in GitHub Desktop.
Goで正規表現にマッチする文字列を取り出すサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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