I've been trying to find a parser or regex that will give me the Android OS version from a user agent string.
E.g.
Mozilla/5.0 (Linux; U; Android 2.2.1; fr-fr; Desire HD Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Will return:
2.2.1
Can anyone help?
May I present an answer that others can easily copy and paste.
Motorola's player user agents can have the following:
So, I've had to start using the the following:
This regular expression is a bit more "future proof" than mathepic's answer:
It allows for multiple digits in each place as well as additional periods in the version number. Android's been out 3 years and we're on 3.0. Eventually we'll get to 10.0.0.
This will catch all of the following:
This could be written a little more strictly as:
This sticks more closely to the schema we've already seen used, but could potentially miss some versions of they decide to add an additional
.1
at the end of a version. It also matches for the future10.0.0
version.Regex should work, something along the untested lines of
Android ([0-9]\.[0-9](\.[0-9])?);
And then use whatever regex function you use to get that part inside the parens.