VersionParser { static VersionNumber identifyIOSVersion(final String userAgent) { VersionNumber version = VersionNumber.UNKNOWN; final List<Pattern> patterns = new ArrayList<Pattern>(); patterns.add(Pattern.compile("iPhone OS\\s?((\\d+)((\\_\\d+)+)?) like Mac OS X")); patterns.add(Pattern.compile("CPU OS\\s?((\\d+)((\\_\\d+)+)?) like Mac OS X")); patterns.add(Pattern.compile("iPhone OS\\s?((\\d+)((\\.\\d+)+)?);")); for (final Pattern pattern : patterns) { final Matcher m = pattern.matcher(userAgent); if (m.find()) { version = parseFirstVersionNumber(m.group(MAJOR_INDEX).replaceAll("_", ".")); break; } } return version; } private  VersionParser(); static VersionNumber parseLastVersionNumber(@Nonnull final String text); static VersionNumber parseOperatingSystemVersion(@Nonnull final OperatingSystemFamily family, @Nonnull final String userAgent); static VersionNumber parseVersion(@Nonnull final String version);  }
@Test public void identifyIOSVersion_versionWithDots() { final String pix51 = "Pixellent 1.5.7 rv:31 (iPad; iPhone OS 5.1; de_DE)"; assertThat(VersionParser.identifyIOSVersion(pix51).toVersionString()).isEqualTo("5.1"); }
@Test public void identifyIOSVersion_versionWithUnderline() { final String ipad = "Mozilla/5.0 (iPad; U; CPU OS 5_1 like Mac OS X; en_us) AppleWebKit/6534.46 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5"; assertThat(VersionParser.identifyIOSVersion(ipad).toVersionString()).isEqualTo("5.1"); final String iphone = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3"; assertThat(VersionParser.identifyIOSVersion(iphone).toVersionString()).isEqualTo("5.1.1"); }