Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Python and Javascript in evaluating a regular expression

First of all, this is not a dupe of this question.

In Javascript this expression seems to be evaluated correctly:

\\/(omniture|mbox|hbx|omniunih)(.*)?

If I pass it to Python re module, bad things happen. In fact, the following returns an error:

import re
re.compile (u'\\/(omniture|mbox|hbx|omniunih)(.*)?')

In [101]: re.compile (u'\\/(omniture|mbox|hbx|omniunih)(.*)?')
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/home/fakk/spider.io/1/<ipython-input-101-b5b19eb3b66e> in <module>()
----> 1 re.compile (u'\\/(omniture|mbox|hbx|omniunih)(.*)?')

/usr/lib/python2.7/re.pyc in compile(pattern, flags)
    188 def compile(pattern, flags=0):
    189     "Compile a regular expression pattern, returning a pattern object."
--> 190     return _compile(pattern, flags)
    191 
    192 def purge():

/usr/lib/python2.7/re.pyc in _compile(*key)
    242         p = sre_compile.compile(pattern, flags)
    243     except error, v:
--> 244         raise error, v # invalid expression
    245     if len(_cache) >= _MAXCACHE:
    246         _cache.clear()

error: nothing to repeat

Python complains about the (.*)? part, which me myself am not able to understand.

My questions are:

  1. What does (.*)? do in JS? Match zero or one (?) of zero or more (*) chars (.)? What's the point?
  2. How can I translate it in Python?
like image 473
Jir Avatar asked Feb 22 '23 23:02

Jir


2 Answers

The question mark is superfluous, as you reflect yourself, it doesn't really make any sense, remove it and you should be in business.

like image 98
zrvan Avatar answered Feb 24 '23 17:02

zrvan


Your regular expression does not make sense, the ? at the end of your string is not needed and will in fact never match anything. In addition I suggest you use r'' to make your expression easier to read:

import re
my_regex = re.compile(r'\/(omniture|mbox|hbx|omniunih)(.*)')
like image 25
NobRuked Avatar answered Feb 24 '23 17:02

NobRuked