Detecting iOS Version Number from User Agent Using Regular Expressions

How to detect iOS 6 and all minor versions by user agent?

CHANGELOG

20/01/2017

  • UA strings update: 141 matching, 0 partially matching, and 797 invalid lines

  • "Mobile Safari 1.1.3 (iPhone U; CPU like Mac OS X; en)" is considered invalid even if it mentions iPhone. Actually a semi-colon is missing after the iPhone term which is suspicious.

  • Safari 8+ doesn't appear yet on UserAgentString.com

PREREQUISITES

  • The following regex is generic.
  • It will match Safari user agent strings (SUAS) running on a mobile device (see below supported devices list).
  • The regex is based on known SUAS by UserAgentString.com as of 20/01/2017.

REGEX

^(?:(?:(?:Mozilla/\d\.\d\s*\()+|Mobile\s*Safari\s*\d+(?:\.\d+)+\s*)(?:iPhone(?:\s+Simulator)?|iPad|iPod);\s*(?:U;\s*)?(?:[a-z]+(?:-[a-z]+)?;\s*)?CPU\s*(?:iPhone\s*)?(?:OS\s*\d+_\d+(?:_\d+)?\s*)?(?:like|comme)\s*Mac\s*O?S?\s*X(?:;\s*[a-z]+(?:-[a-z]+)?)?\)\s*)?(?:AppleWebKit/\d+(?:\.\d+(?:\.\d+)?|\s*\+)?\s*)?(?:\(KHTML,\s*(?:like|comme)\s*Gecko\s*\)\s*)?(?:(?:Version|CriOS)/\d+(?:\.\d+)+\s*)?(?:Mobile/\w+\s*)?(?:Safari/\d+(?:\.\d+)*.*)?$

SUPPORTED DEVICES LIST

  • iPhone
  • iPhone Simulator
  • iPad
  • iPod

EXPLANATION / CUSTOMIZATION

Lines preceded by a C can be customized.

  1. ^(?:(?:(?:Mozilla/\d\.\d\s*\()+|Mobile\s*Safari\s*\d+(?:\.\d+)+\s*)
C 2. (?:iPhone(?:\s+Simulator)?|iPad|iPod);\s*
3. (?:U;\s*)?
4. (?:[a-z]+(?:-[a-z]+)?;\s*)?
5. CPU\s*
6. (?:iPhone\s*)?
C 7. (?:OS\s*\d+_\d+(?:_\d+)?\s*)?
C 8. (?:like|comme)\s*Mac\s*O?S?\s*X
9. (?:;\s*[a-z]+(?:-[a-z]+)?)?
10. \)\s*)?
11. (?:AppleWebKit/\d+(?:\.\d+(?:\.\d+)?|\s*\+)?\s*)?
C 12. (?:\(KHTML,\s*(?:like|comme)\s*Gecko\s*\)\s*)?
C 13. (?:(?:Version|CriOS)/\d+(?:\.\d+)+\s*)?
14. (?:Mobile/\w+\s*)?
15. (?:Safari/\d+(?:\.\d+)*.*)?$

line 1. UA strings may differ so this line is introduced for accepting UA strings as much as possible.

line 2. You can specify here the piped-separated list of accepted devices.

(...)

line 7. The version is indicated here. Change this line if you want a special version. Don't forget to update line 13 too. For instance, matching iOS 5.x.y use (?:OS\s*5_\d+_\d+\s*)?.

line 8. Some user agent strings are translated. The word like may be translated into a foreign language. The regexp now supports English and French. Adapt this line if you encounter other languages. Don't forget to update line 12 too.

(...)

line 12. See line 8.
line 13. See line 7. For instance, matching iOS 5.x.y use (?:Version/5\.\d+\.\d+)?\s*)?.

PHP - Get exact iOS version from User-Agent

Code

There are multiple regular expressions that you can perform to get the OS version. The regex below is likely the fastest:

See regex in use here

ip(?:hone|[ao]d) os \K[\d_]+

Note: These regexes all use the i flag.

Other variations may also be used such as:

i(?:phone|pad|pod) os \K[\d_]+
i(?:phone|pad|pod) os ([\d_]+)

Usage

See code in use here

<?php

$re = '/ip(?:hone|[ao]d) os \K[\d_]+/i';
$str = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2_1 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0 Mobile/15C153 Safari/604.1';
preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0);
var_dump($matches);

Note: To separate each version part in the result you can use the following line as shown here:

var_dump(explode("_", $matches[0][0]));

Usage - With OP's Code

<?php

$re = '/ip(?:hone|[ao]d) os \K[\d_]+/i';
preg_match($re, $_SERVER['HTTP_USER_AGENT'], $matches, PREG_OFFSET_CAPTURE, 0);
var_dump($matches);

Results

Input

Mozilla/5.0 (iPhone; CPU iPhone OS 11_2_1 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0 Mobile/15C153 Safari/604.1

Output

Outputting the match:

11_2_1

Outputing the match with explode() on _ (seen in the note under the first Usage section):

11
2
1

Explanation

  • ip Match this literally
  • (?:hone|[ao]d) Match either of the following

    • hone Match this literally (makes iphone)
    • [ao]d Match either a or o followed by d (makes ipad or ipod)
  • Match a literal space character
  • os Match this literally
  • Match a literal space character. I noticed the OP also had OS\_, which makes me think there may be a possibility for an underscore character here. If that's the case this can be changed to [ _] instead.
  • \K Resets the starting point of the match. Any previously consumed characters are excluded from the final match
  • [\d_]+ Match one or more of any digit or underscore characters

Regex match a version number in a User-Agent (without the name off the application)

Just put the pattern MyApp/ which matches the part before version number inside a positive look-behind assertion.

(?<=MyApp/)\d+(?:\.\d+)+

DEMO

String s = "Mozilla/5.0 (Linux; U; fr-fr; Desire HD Build/FRG83D) **MyApp/2.2.1** AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533";
Matcher m = Pattern.compile("(?<=MyApp/)\\d+(?:\\.\\d+)+").matcher(s);
if(m.find())
{
System.out.println(m.group());
}

Output:

2.2.1

Regular expression to negate chrome on IOS devices

Try to add the negation at the beginning of your code (?!.*CriOS).

Example:

(?!.*CriOS)\\biP\\w+; CPU(?: iP\\w+)? OS (?:1[2-9][_\\d]+|11_(?:\\d\\d+|[3-9])(?:_\\d+)?) like Mac OS X\\b

Check if iOS version Compatiple from iOS 3.0 to IOS 4.0 or higher with PHP

If you still have a problem with ios version number, you can use regex. Detecting iOS Version Number from User Agent using Regular Expressions

Detect iOS version less than 5 with JavaScript

This snippet of code can be used to determine any version of iOS 2.0 and later.

function iOSversion() {
if (/iP(hone|od|ad)/.test(navigator.platform)) {
// supports iOS 2.0 and later: <http://bit.ly/TJjs1V>
var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
}
}

ver = iOSversion();

if (ver[0] >= 5) {
alert('This is running iOS 5 or later.');
}

regex in C language to pull chrome version out of user agent

Try this:

regcomp(&re, "Chrome\\/[0-9]+[.[0-9]+]*", REG_EXTENDED)

You can get more info about regex.h here:
https://pubs.opengroup.org/onlinepubs/7908799/xsh/regex.h.html



Related Topics



Leave a reply



Submit