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 Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Golang 单实例实现网站多域名请求

有时候写网站,为了统一的后端,把不同业务都集中到一个后端,这时就需要处理多域名的请求,在 Go http server 里实现很简单,只需把不同域名映射到不同的 `http.Handler`。...

Golang sync.WaitGroup 的 Wait 超时处理

sync.WaitGroup 使用 `Add(1)`、`Done()`、`Wait()`组合来实现多协程等待,如果某一协程未能合理处理错误,导致无法退出,此时需要引入超时机制。下面是一种超时处理方法。...

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

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