0

All I need to do is to convert the following Javascript regex test to Python:

var is_ios_app = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);
Jerry
  • 70,495
  • 13
  • 100
  • 144
garbanzio
  • 846
  • 1
  • 7
  • 10
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/about) to learn how to write effective questions. – Jerry Mar 31 '14 at 19:39
  • I am merely asking the same question as this: http://stackoverflow.com/questions/4460205/detect-ipad-iphone-webview-via-javascript but I couldn't find any good solutions anywhere on the interwebs to do the same thing in python. – garbanzio Mar 31 '14 at 21:00
  • I think the most basic thing to do would be to look for how python uses regular expressions yes? That would have led you to [this page](https://docs.python.org/2/library/re.html) using google, detailing all the different functions you can use and how to use them. – Jerry Apr 01 '14 at 05:19

2 Answers2

1

There is python library which can use for that purpose.

https://pypi.python.org/pypi/httpagentparser

Example Usage:

>>> import httpagentparser
>>> s = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.9 (KHTML, like Gecko) \
        Chrome/5.0.307.11 Safari/532.9"
>>> print httpagentparser.detect(s)
{'dist': {'version': '2.3.5', 'name': 'Android'},
'os': {'name': 'Linux'},
'browser': {'version': '4.0', 'name': 'Safari'}}

So you can use name element of dist to get ios users.

Mehmet Ince
  • 1,298
  • 9
  • 20
0

Use re.search() for this in Python.

userAgent = "YOUR_UA"
pattern = '(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)'
if(re.search(pattern, userAgent)):
    print "true"
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85