【发布时间】:2016-05-07 09:32:23
【问题描述】:
我正在使用正则表达式在字符串中查找主机名,该字符串匹配所有可能的组合,但无法构造一个。
names = ['www.google.com.in','w.stack.in','www.code31ws.com','google.com','ww.sample.co']
regex = '(w{3}?\.?)?[\w?-]+\.(com|in|edu|co)'
for i in range(len(ips)):
reg = re.search(regex,ips[i])
if reg:
print "true {}".format(i)
else:
print "false {}".format(i)
结果:
true 0
true 1
true 2
true 3
true 4
希望它不匹配:
w.stack.in
ww.sample.com
【问题讨论】:
-
请注意,
w.stack.in和ww.sample.com也是有效的主机名。 -
考虑
urlparse库。 -
w.stack.in 甚至是一个真实的网站。
标签: python regex python-2.7