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:
(.*)?
do in JS? Match zero or one (?
) of zero or more (*
) chars (.
)? What's the point?The question mark is superfluous, as you reflect yourself, it doesn't really make any sense, remove it and you should be in business.
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)(.*)')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With