|      1besto      2014-12-11 00:59:12 +08:00  1 nginx完全可以把带3w和不带3w的解到不同的网页上去啊。。。 | 
|  |      2NathanInMac      2014-12-11 01:01:36 +08:00  1 | 
|  |      3oott123      2014-12-11 01:08:50 +08:00 via Android  1 domain[0:3] == "www" | 
|  |      4little_cup      2014-12-11 01:09:16 +08:00  1 if domain[:3] == 'www': xxx elif '.com' in domain: yyy else zzz | 
|  |      5oott123      2014-12-11 01:09:39 +08:00 via Android 不对,应该是domain[0:4] == "www." | 
|  |      6imkh OP @NathanInMac 正则还没学,是 m = re.match(r'^((?<subdomain>.+?)\.)*(?<domain>[^\.]*)$','domain') 这样用吗? | 
|  |      7imkh OP @little_cup 这样好像不行吧,如果有info,io这些,那岂不是要多次判断? | 
|  |      8ericls      2014-12-11 03:45:27 +08:00  2 if domain.startswith('www.') | 
|  |      9viesong      2014-12-11 06:53:23 +08:00  1 正则表达式 | 
|  |      10viesong      2014-12-11 06:55:05 +08:00 domain = /^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/; | 
|  |      12thedevil5032      2014-12-11 09:04:19 +08:00  1 判别字符串开头用 @ericls 提到的 str.startswith, 末尾的话 (xxx) 用 str.endswith. | 
|  |      13lll9p      2014-12-11 09:36:25 +08:00 def info_func(): pass def com_func(): pass def net_func(): pass ... xxx={'info':info_func,'com':com_func,'net':net_func} domain = raw_input("Please input an domain: ") if domain.startswith('www'): 执行操作 else: xxx[domain.split('.')[-1]]() | 
|  |      14lll9p      2014-12-11 09:44:49 +08:00  1 空格被吃了。。改一下吧 def info_func(): pass def com_func(): pass def net_func(): pass ... xxx={'info':info_func,'com':com_func,'net':net_func} domain = raw_input("Please input an domain: ") if domain.startswith('www.'): 执行操作 else: try: xxx[domain.split('.')[-1]]() except: pass | 
|      15besto      2014-12-11 10:29:13 +08:00 @NathanInMac 我把问题想复杂了。。。 | 
|  |      164everLoveU      2014-12-11 11:38:23 +08:00 直接domain[0:4] == 'www.' 不就可以了 楼上越搞越复杂 | 
|  |      17FrankFang128      2014-12-11 12:33:49 +08:00 via Android ugly | 
|      18yangzh      2014-12-11 14:34:55 +08:00 楼上用正则的,除了炫技似乎也没啥必要 | 
|  |      19dingyaguang117      2014-12-11 18:32:54 +08:00 看有几个 . ? | 
|  |      20thedevil5032      2014-12-12 08:02:17 +08:00  1 @oott123  @little_cup @4everLoveU domain[0:4] 和 startswith 的区别在于: 1. domain == '' 的时候, 前者会出现 IndexError 异常. startswith 会返回 False. 2. startswith 的 意图更加明显, 更易读. 3. startswith 会慢一点点. 参考: http://stackoverflow.com/questions/1315559/how-good-is-startswith |