你想要一个否定的前瞻断言:
^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>