推荐学习书目
› 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
kingwkb
V2EX  ›  Python

用户名验证问题

  •  
  •   kingwkb · Feb 19, 2012 · 4838 views
    This topic created in 5397 days ago, the information mentioned may be changed or developed.
    用户名至少3个字母或者2个汉字,该如何验证呢?
    15 replies  •  1970-01-01 08:00:00 +08:00
    fanzeyi
    fanzeyi
    Feb 19, 2012
    1
    def unicode_len(text):
    if isinstance(text, str):
    return len(text.encode('utf-8'))
    return len(text)

    俺是这么写的
    kingwkb
    kingwkb
    OP
    Feb 19, 2012
    2
    @fanzeyi

    >>> u = '中文'
    >>> isinstance(u,str)
    True
    >>> u = 'aaaa'
    >>> isinstance(u,str)
    True
    >>> u = u'中文'.encode('utf8')
    >>> isinstance(u,str)
    True
    >>> u = u'中文'.encode('gbk')
    >>> isinstance(u,str)

    这都一个样。。。
    iwinux
    iwinux
    Feb 19, 2012
    3
    大致思路是这样的:

    pattern = re.compile(r'^([A-Za-z]{3}|{han}{2})$')
    valid = pattern.match(name)

    可惜的是 Python 的正则没法直接匹配汉字(Ruby 里可以 = =),需要做一些额外处理,可以参考 Stack Overflow 上的这个问答:http://stackoverflow.com/questions/2718196/how-to-find-all-chinese-text-in-a-string-using-python
    fanzeyi
    fanzeyi
    Feb 19, 2012
    4
    ....显然是要都化成Unicode然后len就是长度了。。。

    In [5]: len(u'测试')
    Out[5]: 2

    In [6]: len('测试')
    Out[6]: 6
    fanzeyi
    fanzeyi
    Feb 19, 2012
    5
    哦我错了应该是decode...
    fanzeyi
    fanzeyi
    Feb 19, 2012
    6
    In [13]: unicode_len(u'测试')
    Out[13]: 2

    In [14]: unicode_len(u'aaa')
    Out[14]: 3

    In [15]: unicode_len('aaa')
    Out[15]: 3

    In [16]: unicode_len('测试')
    Out[16]: 2

    In [17]: unicode_len('测试aa')
    Out[17]: 4

    In [18]: unicode_len(u'测试aa')
    Out[18]: 4
    zythum
    zythum
    Feb 19, 2012
    7
    经许浩大人调教
    /[a-zA-Z]{3}|[\u4e00-\u9fa5]{2}/

    应该没问题了
    kingwkb
    kingwkb
    OP
    Feb 19, 2012
    8
    @fanzeyi 用unicode的话,中文和字母都是1,怎么判断中文至少2个,英文3个

    比如
    用户名是“中文”,这个可以注册,2个中文

    用户名是“中”,这个不可以注册

    用户名是“aa”,这个不可以注册,英文至少3个
    fanzeyi
    fanzeyi
    Feb 19, 2012
    9
    @kingwkb 哦。。 我错了。。 我以为是合起来的。。 =.=
    kingwkb
    kingwkb
    OP
    Feb 19, 2012
    10
    @zythum 这还是有缺陷

    比如:
    用户名是“中aa”,这个应该是可以通过的,一个中文可以当2个英文,所以符合英文3+
    kingwkb
    kingwkb
    OP
    Feb 19, 2012
    11
    pattern = re.compile(u'([\u4e00-\u9fa5])')

    m = pattern.findall(u'中文dfasdf')
    m
    [u'\u4e2d', u'\u6587']

    >>> pattern = re.compile(u'([a-zA-z0-9])')
    >>> m = pattern.findall(u'中文dfasdf')
    >>> m
    [u'd', u'f', u'a', u's', u'd', u'f']

    好吧,这样解决,分开匹配,m相加
    kingwkb
    kingwkb
    OP
    Feb 19, 2012
    12
    这样还有一点不爽是网页提交的是utf8或者gbk,要先转成unicode,哪位还有更好的办法么?
    iwinux
    iwinux
    Feb 19, 2012
    13
    @kingwkb 这样可以么:

    chinese = re.compile('^[\u4e00-\u9fa5]$')
    english = re.compile('^[A-Za-z]$')

    chinese_count = 0
    english_count = 0

    for s in name:
    if chinese.match(s):
    chinese_count += 1
    elif english.match(s):
    english_count += 1

    valid = (chinese_count >= 2) or (english_count >= 3) or (chinese_count + english_count >= 3)
    zythum
    zythum
    Feb 19, 2012
    14
    @kingwkb 朱一只能测试js,js表示'中aa'是通过不了了...正则不是通用的么
    kingwkb
    kingwkb
    OP
    Feb 20, 2012
    15
    @zythum 这个需要用unicode编码,javascript应该不是unicode,具体的不清楚,可能不是utf8就是gbk,跟网页用的编码有关?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   865 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:59 · PVG 04:59 · LAX 12:59 · JFK 15:59
    ♥ Do have faith in what you're doing.