golang 用正则包regexp 通过user-agent 识别手机浏览器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import (
"fmt"
"regexp"
)
func main() {
userAgent := "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53"
mobileRe, _ := regexp.Compile("(?i:Mobile|iPod|iPhone|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP)")
fmt.Printf("%q\n", mobileRe.FindString(userAgent))
userAgent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
fmt.Printf("%q\n", mobileRe.FindString(userAgent))
}
输出:
1
2
"iPhone"
""
本文网址: https://golangnote.com/topic/15.html 转摘请注明来源