VersionParser { static VersionNumber identifyWindowsVersion(final String userAgent) { VersionNumber version = VersionNumber.UNKNOWN; final List<Pattern> patterns = new ArrayList<Pattern>(); patterns.add(Pattern.compile("Windows NT\\s?((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("Windows Phone OS ((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("Windows CE ((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("Windows 2000\\s?((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("Windows XP\\s?((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("Windows 7\\s?((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("Win 9x ((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("Windows ((\\d+)((\\.\\d+)+)?)")); patterns.add(Pattern.compile("WebTV/((\\d+)((\\.\\d+)+)?)")); for (final Pattern pattern : patterns) { final Matcher m = pattern.matcher(userAgent); if (m.find()) { version = parseFirstVersionNumber(m.group(MAJOR_INDEX)); 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 identifyWindowsVersion() { final String windows31 = "Mozilla/2.0 (compatible; MSIE 3.0b; AOL 4.0; Windows 3.1)"; assertThat(VersionParser.identifyWindowsVersion(windows31).toVersionString()).isEqualTo("3.1"); final String windows95_1 = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 95)"; assertThat(VersionParser.identifyWindowsVersion(windows95_1).toVersionString()).isEqualTo("95"); final String windows95_2 = "Mozilla/3.01 ( compatible; [de]; Windows 95; win9x/NT 4.90 )"; assertThat(VersionParser.identifyWindowsVersion(windows95_2).toVersionString()).isEqualTo("95"); final String windows98 = "Mozilla/3.01 ( compatible; [en]; Windows 98; Compaq )"; assertThat(VersionParser.identifyWindowsVersion(windows98).toVersionString()).isEqualTo("98"); final String windowsNT = "Mozilla/4.0 (compatible ; MSIE 5.5 SP2 ; Windows NT 4.0 SP6a ; IC-Client)"; assertThat(VersionParser.identifyWindowsVersion(windowsNT).toVersionString()).isEqualTo("4.0"); final String windows2000 = "Mozilla/5.0 (Windows; U; Windows NT 5.0; de-DE; rv:1.0.2) Gecko/20030208 Netscape/7.02"; assertThat(VersionParser.identifyWindowsVersion(windows2000).toVersionString()).isEqualTo("5.0"); final String windowsXP = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SLCC1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30707; InfoPath.2)"; assertThat(VersionParser.identifyWindowsVersion(windowsXP).toVersionString()).isEqualTo("5.1"); final String windows2003Server = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; WOW64; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)"; assertThat(VersionParser.identifyWindowsVersion(windows2003Server).toVersionString()).isEqualTo("5.2"); final String windowsVista = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; InfoPath.2; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)"; assertThat(VersionParser.identifyWindowsVersion(windowsVista).toVersionString()).isEqualTo("6.0"); final String windows7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; Trident/4.0; GTB7.2; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; Zune 4.7)"; assertThat(VersionParser.identifyWindowsVersion(windows7).toVersionString()).isEqualTo("6.1"); final String windowsPhone7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; HTC; 7 Mozart; Orange)"; assertThat(VersionParser.identifyWindowsVersion(windowsPhone7).toVersionString()).isEqualTo("7.0"); }