19 lines
399 B
Go
19 lines
399 B
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"regexp"
|
||
)
|
||
|
||
var namedRegex = regexp.MustCompile(`姓名:(?P<name>[\p{Han}a-zA-Z0-9]+),城市:(?P<city>[\p{Han}a-zA-Z0-9]+)`)
|
||
|
||
func main() {
|
||
text := "姓名:孙三苗,城市:北京"
|
||
match := namedRegex.FindStringSubmatch(text)
|
||
for i, name := range namedRegex.SubexpNames() {
|
||
if i > 0 && name != "" {
|
||
fmt.Printf("%s:%s\n", name, match[i])
|
||
}
|
||
}
|
||
}
|