你要负先行断言:
^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