V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
imkh
V2EX  ›  Python

请教各位大神,如何判断用户输入的是 www.domain.xxx 还是 domain.xxx

  •  
  •   imkh · 2014-12-11 00:54:59 +08:00 · 3879 次点击
    这是一个创建于 3425 天前的主题,其中的信息可能已经有所发展或是发生改变。
    xxx:表示com,net,info等

    domain = raw_input("Please input an domain: ")
    if 是www.domain.xxx:
    执行操作
    elif 是domain.xxx:
    执行操作
    else
    执行操作
    20 条回复    2014-12-12 08:02:17 +08:00
    besto
        1
    besto  
       2014-12-11 00:59:12 +08:00   ❤️ 1
    nginx完全可以把带3w和不带3w的解到不同的网页上去啊。。。
    NathanInMac
        2
    NathanInMac  
       2014-12-11 01:01:36 +08:00   ❤️ 1
    @besto 。。你这完全不看帖的啊,人家是form里面。要用正则

    ^((?<subdomain>.+?)\.)*(?<domain>[^\.]*)$
    oott123
        3
    oott123  
       2014-12-11 01:08:50 +08:00 via Android   ❤️ 1
    domain[0:3] == "www"
    little_cup
        4
    little_cup  
       2014-12-11 01:09:16 +08:00   ❤️ 1
    if domain[:3] == 'www':
    xxx
    elif '.com' in domain:
    yyy
    else
    zzz
    oott123
        5
    oott123  
       2014-12-11 01:09:39 +08:00 via Android
    不对,应该是domain[0:4] == "www."
    imkh
        6
    imkh  
    OP
       2014-12-11 01:22:46 +08:00
    @NathanInMac 正则还没学,是 m = re.match(r'^((?<subdomain>.+?)\.)*(?<domain>[^\.]*)$','domain') 这样用吗?
    imkh
        7
    imkh  
    OP
       2014-12-11 01:23:52 +08:00
    @little_cup 这样好像不行吧,如果有info,io这些,那岂不是要多次判断?
    ericls
        8
    ericls  
       2014-12-11 03:45:27 +08:00   ❤️ 2
    if domain.startswith('www.')
    viesong
        9
    viesong  
       2014-12-11 06:53:23 +08:00   ❤️ 1
    正则表达式
    viesong
        10
    viesong  
       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})+\.?/;
    Amit
        11
    Amit  
       2014-12-11 08:56:08 +08:00
    @besto 昨天才发现www.v2ex.com和v2ex.com不是一个ip
    thedevil5032
        12
    thedevil5032  
       2014-12-11 09:04:19 +08:00   ❤️ 1
    判别字符串开头用 @ericls 提到的 str.startswith, 末尾的话 (xxx) 用 str.endswith.
    lll9p
        13
    lll9p  
       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]]()
    lll9p
        14
    lll9p  
       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
    besto
        15
    besto  
       2014-12-11 10:29:13 +08:00
    @NathanInMac 我把问题想复杂了。。。
    4everLoveU
        16
    4everLoveU  
       2014-12-11 11:38:23 +08:00
    直接domain[0:4] == 'www.' 不就可以了

    楼上越搞越复杂
    FrankFang128
        17
    FrankFang128  
       2014-12-11 12:33:49 +08:00 via Android
    ugly
    yangzh
        18
    yangzh  
       2014-12-11 14:34:55 +08:00
    楼上用正则的,除了炫技似乎也没啥必要
    dingyaguang117
        19
    dingyaguang117  
       2014-12-11 18:32:54 +08:00
    看有几个 . ?
    thedevil5032
        20
    thedevil5032  
       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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   913 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:31 · PVG 05:31 · LAX 14:31 · JFK 17:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.