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 quicktemplate 模版快速入门

Golang 有很多的模版引擎,自带的 `html/template` 也很好,大多数情况都能满足需求,只是有些逻辑、条件判断不好在模版里实现, `quicktemplate` 是个很好的选择。...

golang 实现的基于web的文件管理-filebrowser

FileBrowser 在指定目录中提供了一个文件管理界面,可用于上传,删除,预览,重命名和编辑文件。它允许创建多个用户,每个用户都可以有自己的目录。它可以用作独立的应用程序。...

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.20 Processed in 0ms