从用户代理获取Android OS版本 [英] Get Android OS version from user-agent

查看:113
本文介绍了从用户代理获取Android OS版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试寻找一种解析器或正则表达式,该解析器或正则表达式将从用户代理字符串中为我提供Android OS版本.

I've been trying to find a parser or regex that will give me the Android OS version from a user agent string.

例如

Mozilla/5.0 (Linux; U; Android 2.2.1; fr-fr; Desire HD Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

会返回:

2.2.1

任何人都可以帮忙吗?

推荐答案

此正则表达式比它允许在每个位置使用多个数字以及版本号中的其他句点. Android已经淘汰了3年,而我们已经推出了3.0版.最终我们将达到10.0.0.

It allows for multiple digits in each place as well as additional periods in the version number. Android's been out 3 years and we're on 3.0. Eventually we'll get to 10.0.0.

这将捕获以下所有内容:

This will catch all of the following:

  • 1.6(真实)
  • 2.3.4(真实)
  • 9
  • 12.34.56(假,但是有一天……)
  • 3.4.5.6和4.5.6.7.8.9(伪造...但以防万一)

这可以写得更严格一些:

This could be written a little more strictly as:

Android (\d+(?:\.\d+){0,2});

这与我们已经看到的使用过的模式更加紧密,但是可能会遗漏某些版本,因此他们决定在版本末尾添加一个额外的.1.它还与将来的10.0.0版本匹配.

This sticks more closely to the schema we've already seen used, but could potentially miss some versions of they decide to add an additional .1 at the end of a version. It also matches for the future 10.0.0 version.

这篇关于从用户代理获取Android OS版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆