Reputation: 5163
I am trying to create a regex for the following UA string:
Mozilla/5.0 (BlackBerry; U; BlackBerry 9850; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.115 Mobile Safari/534.11+
I want to know if the device is a Blackberry 5.0 so I can create a non ajax jquery mobile site.
I can get the Mozilla/5.0 bit ok but im really struggling to match the word Blackberry.
Can anyone help?
Upvotes: 0
Views: 680
Reputation: 6721
According to this administrator's Blackberry Support Community Forums Post, AJAX support for BlackBerry phones was released with version 4.6
According to this list, BlackBerry UA Strings have always contained the word BlackBerry, have usually contained the phone model number, and have always contained a version number:
Mozilla/5.0 (BlackBerry; U; BlackBerry 9860; en-GB) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.296 Mobile Safari/534.11+
Mozilla/5.0 (BlackBerry; U; BlackBerry 9300; fr) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.570 Mobile Safari/534.8+
Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.600 Mobile Safari/534.8+
Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.246 Mobile Safari/534.1+
Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+
Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-US) AppleWebKit/530.17 (KHTML, like Gecko) Version/6.0.0.62 Mobile Safari/530.17
BlackBerry9650/5.0.0.732 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/105
BlackBerry9700/5.0.0.351 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/123
BlackBerry9630/4.7.1.40 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/105
BlackBerry9000/4.6.0.167 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102
BlackBerry8330/4.3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/105
BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLOC-1.1 VendorID/105
BlackBerry8820/4.2.2 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102
BlackBerry8703e/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/105
BlackBerry8320/4.5.0.188 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/100
BlackBerry8330/4.3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/106
BlackBerry8320/4.3.1 Profile/MIDP-2.0 Configuration/CLDC-1.1
BlackBerry8110/4.3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/118
BlackBerry8130/4.5.0.89 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/106
BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103
BlackBerry7130e/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/104
BlackBerry7250/4.0.0 Profile/MIDP-2.0 Configuration/CLDC-1.1
BlackBerry/3.6.0
BlackBerry7230/3.7.0
BlackBerry7230/3.7.1
BlackBerry7730/3.7.0
BlackBerry7730/3.7.1 UP.Link/5.1.2.5
the version number comes either after the word BlackBerry
, possibly a model number consisting of numbers and letters, then a forward slash (/
), or it comes well after the word BlackBerry
, but immediately following the string Version/
Using this expression:
BlackBerry(\w*/|.*?Version/)(((?:[0-3]|4\.[0-5])\.[.\d+]+)|((?:4\.[6-9]|1?[5-9])[.\d+]+)|([\w.]+))?
In a find-type regular expression parser (like PHP's preg_match()
, .Net's Regex.Match()
, or Java's matcher.find()
functions) this expression will allow discerning between a version number from 0.0.X
to 4.5.X
and a version number from 4.6.X
to X.X.X
where X
represents any number not previously matched.
What's that now? Sorry... in other words, using that regex against a user agent string should allow you to determine whether it's a BlackBerry browser or not AND whether the version number indicates support for AJAX (pseudo-code):
regex = "BlackBerry(\w*/|.*?Version/)(((?:[0-3]|4\.[0-5])\.[.\d+]+)|((?:4\.[6-9]|1?[5-9])[.\d+]+)|([\w.]+))?";
result = regex.find(UserAgentString);
if (result.matchFound)
{
actualVersion = result.matchGroup(2);
if (result.matchGroup(3) != "")
{
print("Version " + actualVersion + " does not support AJAX");
}
else if (result.matchGroup(4) != "")
{
print("Version " + actualVersion + " supports AJAX!");
}
else if (result.matchGroup(5) != "")
{
print("Unknown whether Version " + actualVersion + " supports AJAX!?!?");
}
}
print("Not A BlackBerry Browser");
Summary: Match group #1 (could be made optional) matches the part between BlackBerry
and any matched version number. Group #2 matches the version number. Group #3 contains the version number if it is 0.0.X
to 4.5.X
. Group #4 contains the version number if it is 4.6.X
or greater, if it only consists of digits and decimal points. If the version does not seem to match this convention, possibly if there are letters or underscores as well, then it will be captured into Group #5.
I think this is all you need (once translated into whichever language you are using). The expression should be supported by .Net, Java, PHP, or even JavaScript if necessary.
Upvotes: 2
Reputation: 11610
If you just need to make sure that the term Blackberry
is in the text, you could just do a simple text search (doesn't even need to be regex):
Blackberry
If you need to make sure that it comes after the Mozilla/5.0
bit, then you could just use this regex:
^Mozilla/5\.0 \(BlackBerry
Upvotes: 0