GolangNote

Golang笔记

golang 用正则包regexp 通过user-agent 识别手机浏览器

Permalink

golang 用正则包regexp 通过user-agent 识别手机浏览器

golang 用正则包regexp 通过user-agent

Go: 分析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))
}

输出:

plaintext: 输出
1
2
"iPhone"
""

本文网址: https://golangnote.com/topic/15.html 转摘请注明来源

Related articles

Golang http client 处理重定向网页

假设一个网址有多个重定向,A-B-C-D,使用 http.Client.Get 最后取得的内容是网址D的内容,我们该手动处理包含重定向的网址。...

谷歌翻译的 golang 库推荐

Google 的翻译越来越好了,官方的Golang SDK 已经很完美,这里介绍的是几个非官方发布的有特色的库。...

Write a Comment to "golang 用正则包regexp 通过user-agent 识别手机浏览器"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms