推荐学习书目
› 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 · Dec 11, 2014 · 5052 views
    This topic created in 4349 days ago, the information mentioned may be changed or developed.
    xxx:表示com,net,info等

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

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

    楼上越搞越复杂
    FrankFang128
    FrankFang128
    Dec 11, 2014 via Android
    17
    ugly
    yangzh
    yangzh
    Dec 11, 2014
    18
    楼上用正则的,除了炫技似乎也没啥必要
    dingyaguang117
    dingyaguang117
    Dec 11, 2014
    19
    看有几个 . ?
    thedevil5032
    thedevil5032
    Dec 12, 2014❤️1
    20
    @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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1463 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 15:44 · PVG 23:44 · LAX 07:44 · JFK 10:44
    ♥ Do have faith in what you're doing.