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 泛型性能初识

编程时,我们通常需要编写“泛型”函数,其中确切的数据类型并不重要。例如,您可能想编写一个简单的函数来对数字进行求和。Go 直到最近才有这个概念,但最近才添加了它(从1.18版开始)。...

Golang http IPv4/IPv6 服务

Golang 的网络服务,如果不指定IPv4 或 IPv6,如果VPS 同时支持 IPv4 和 IPv6,`net.Listen()` 只会监听 IPv6 地址。但这不影响客户端使用 IPv4 地址来访问。...

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

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