使用正则表达式从用户代理检测iOS版本号 [英] Detecting iOS Version Number from User Agent using Regular Expressions
问题描述
如果我有iOS用户代理,例如
If I have an iOS user-agent like
Mozilla / 5.0(iPhone; U; CPU iPhone OS 4_0,如Mac OS X ; en-us)AppleWebKit / 532.9(KHTML,与Gecko一样)Version / 4.0.5 Mobile / 8A293 Safari / 6531.22.7
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
或
Mozilla / 5.0(iPhone; U; CPU iPhone OS 4_0,如Mac OS X; en-us)AppleWebKit / 532.9(KHTML,像Gecko)Mobile / 7D11
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/7D11
我如何使用正则表达式检测iOS版本,以便它返回,例如
How would I detect the iOS version using regular expressions so that it would return e.g.
4.0
4.0
上述用户代理?
推荐答案
为我工作的RegEx是:
The RegEx that is working for me is:
/OS ((\d+_?){2,3})\s/
iPad 5.1.1有 HTTP_USER_AGENT
,如下所示:
An iPad 5.1.1 has an HTTP_USER_AGENT
like this:
"Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3"
所以 3个数字iOS版和不是iPhone OS 目前。
可能的Ruby实现可能是:
A possible Ruby implementation could be:
def version_from_user_agent(user_agent)
version = user_agent.match(/OS ((\d+_?){2,3})\s/)
version = version[1].gsub("_",".") if version && version[1]
end
如果不是,则返回 nil 找到iOS版本
It returns nil if not iOS version found
这篇关于使用正则表达式从用户代理检测iOS版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!