开发者

Python Regex to parse apart Blackberry browser user agent

I need to parse apart blackberry browser user agents so I can get what device and version it is using python 2.5. For example:

BlackBerry9630/4.7.1.65 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/-1,gzip(gfe),gzip(gfe)

In the above user agent I would like to gather the following groups:

Browser: Blackbe开发者_如何学Crry 
Device: 9630 
Major Version: 4 
Minor Version: 7

The rest of the info I don't care about.

Here is another example:

BlackBerry9530/5.0.0.328 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/105,gzip(gfe),gzip(gfe),gzip(gfe)
Browser: Blackberry
Device: 9530
Major Version: 5
Minor Version: 0

I'm horrible at figuring out regex and any help would be great. Thanks


Something like this will work for your case, but not necessarily all cases:

'^(\D*)(\d*)/(\d*)\.(\d*)\.'

\D means "any character that's not a decimal digit", and \d means "any decimal digit".


browser, version = useragent.split()[0].split("/")
browsername = re.findall(r"[a-zA-Z]+", browser)
device = re.findall(r"[0-9]+", browser)
versions = re.findall(r"[0-9]+", version)
major = versions[0]
minor = versions[1]


>>> import re
>>> s = 'BlackBerry9530/5.0.0.328 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/105,gzip(gfe),gzip(gfe),gzip(gfe)'
>>> print(re.compile(r'(Blackberry)(\d+)/(\d+)\.(\d+)\.',re.I).search(s).groups())
('BlackBerry', '9530', '5', '0')
>>>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜