首先,這不是this question的騙局。Python和Javascript之間在評估正則表達式上的區別
在Javascript中這樣表達似乎正確地評價:
\\/(omniture|mbox|hbx|omniunih)(.*)?
如果我把它傳遞到Python re
模塊,不好的事情發生。事實上,以下將返回一個錯誤:
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抱怨的(.*)?
部分,這我自己也無法理解。
我的問題是:
- 是什麼
(.*)?
做JS?匹配零個或多個(?
)零個或多個(*
)個字符(.
)?重點是什麼? - 如何在Python中翻譯它?
「Python抱怨(。*)?部分」 - 你能發佈錯誤信息嗎? (我想這是「錯誤:沒有重複」,對吧?) – 2012-01-04 10:38:17
當然,我會更新文章 – Jir 2012-01-04 10:39:11
你釘了它:)其實,沒有什麼可重複的。其他表達失敗的是:'\/webtrends(。*)?\。js','foresee-(trigger(。*)?| alive | analytics(。*)?)\。js','everestjs \ .net |像素([0-9] *)\ everesttech \ .net'?。 – Jir 2012-01-04 10:45:32