Java正则表达式通过User-Agent获取IOS版本

如果ios版本为 14_3_1

matcher.group(0) 为 CPU iPhone OS 14_3_1 like Mac OS

matcher.group(1) 为 14_3_1

matcher.group(2) 为 3_1

 public Integer getIosVersion(){
         ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
         HttpServletRequest request = attributes.getRequest();
         String userAgent = request.getHeader("User-Agent");
         //region 获取ios大版本 如14 15
         Pattern pattern = Pattern.compile("CPU iPhone OS (.*?)_(.*?) like Mac OS");
         Matcher matcher = pattern.matcher(userAgent);
         Integer iosVersion = null;
         if (matcher.find()){
             String version = matcher.group(1);
             iosVersion = Integer.valueOf(version);
         }
         //endregion
     
         return iosVersion;
 }  

你可能感兴趣的:(Java,java,正则表达式,ios)