Skip to content

Commit c6e9222

Browse files
author
胡子豪
authored
Update zh/07.3.md
more reable code
1 parent 72d959b commit c6e9222

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

zh/07.3.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ func MatchString(pattern string, s string) (matched bool, error error)
2121
如果要验证一个输入是不是IP地址,那么如何来判断呢?请看如下实现
2222
```Go
2323

24-
func IsIP(ip string) (b bool) {
25-
if m, _ := regexp.MatchString("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", ip); !m {
26-
return false
27-
}
28-
return true
24+
func IsIP(ip string) (m bool) {
25+
m, _ = regexp.MatchString("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", ip)
26+
return
2927
}
3028
```
3129
可以看到,`regexp`的pattern和我们平常使用的正则一模一样。再来看一个例子:当用户输入一个字符串,我们想知道是不是一次合法的输入:
@@ -35,7 +33,8 @@ func main() {
3533
if len(os.Args) == 1 {
3634
fmt.Println("Usage: regexp [string]")
3735
os.Exit(1)
38-
} else if m, _ := regexp.MatchString("^[0-9]+$", os.Args[1]); m {
36+
}
37+
if m, _ := regexp.MatchString("^[0-9]+$", os.Args[1]); m {
3938
fmt.Println("数字")
4039
} else {
4140
fmt.Println("不是数字")

0 commit comments

Comments
 (0)