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 IPv4/IPv6 服务

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

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

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

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