你要負先行斷言:
^https?://(?!(?:www\.)?site\.com).+
其中給出:
>>> testdata = '''\
... /page.html => false
... http://www.google.fr => true
... http://site.com => false
... http://site.com/page.html => false
... '''.splitlines()
>>> not_site_com = re.compile(r'^https?://(?!(?:www\.)?site\.com).+')
>>> for line in testdata:
... match = not_site_com.search(line)
... if match: print match.group()
...
http://www.google.fr => true
請注意,該模式不包括www.site.com
和site.com
:
>>> not_site_com.search('https://www.site.com')
>>> not_site_com.search('https://site.com')
>>> not_site_com.search('https://site-different.com')
<_sre.SRE_Match object at 0x10a548510>
''^字符以外類意味着 「行的開始」,而不是 「不」。 – geoffspear 2013-03-27 15:47:33
你可以發表一個你期望/不匹配的例子,但是沒有?正則表達式看起來是合理的。也沒有必要轉義'/'。 – FatalError 2013-03-27 15:49:23