Crossin的编程教室

标题: [正则表达式] 问题 [打印本页]

作者: Charlene97    时间: 2017-12-29 00:55
标题: [正则表达式] 问题
刚刚学了正则表达式1-5, 感觉还是有点似懂非懂. 想写点东西来练练手:

我想检测用户输入的出生年月日是否格式正确, 但是没成功.

import re
bday = input("Please enter your date of birth(mm/dd/yyyy): ")
m = re.findall("^\d{2}+\/+\d{2}+\/+\d{4}$", bday)
if m:
    print (m)
else:
    print ("Invalid input")


作者: Charlene97    时间: 2017-12-29 00:56
这是错误报告, 我认真看了, 完全看不懂... \d{2}表示2个数字, \/我想表示"/"这个符号. 请问是错在哪里呢? 谢谢大家!
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-19-16b473c8f9ae> in <module>()
     13
     14
---> 15 LifeClock()

<ipython-input-19-16b473c8f9ae> in LifeClock()
      4 def LifeClock():
      5     bday = input("Please enter your date of birth(mm/dd/yyyy): ")
----> 6     m = re.findall("^\d{2}+\/+\d{2}+\/+\d{4}$", bday)
      7     if m:
      8         print (m)

/mnt/lovelace/software/anaconda/envs/py35/lib/python3.5/re.py in findall(pattern, string, flags)
    211
    212     Empty matches are included in the result."""
--> 213     return _compile(pattern, flags).findall(string)
    214
    215 def finditer(pattern, string, flags=0):

/mnt/lovelace/software/anaconda/envs/py35/lib/python3.5/re.py in _compile(pattern, flags)
    291     if not sre_compile.isstring(pattern):
    292         raise TypeError("first argument must be string or compiled pattern")
--> 293     p = sre_compile.compile(pattern, flags)
    294     if not (flags & DEBUG):
    295         if len(_cache) >= _MAXCACHE:

/mnt/lovelace/software/anaconda/envs/py35/lib/python3.5/sre_compile.py in compile(p, flags)
    534     if isstring(p):
    535         pattern = p
--> 536         p = sre_parse.parse(p, flags)
    537     else:
    538         pattern = None

/mnt/lovelace/software/anaconda/envs/py35/lib/python3.5/sre_parse.py in parse(str, flags, pattern)
    827     pattern.str = str
    828
--> 829     p = _parse_sub(source, pattern, 0)
    830     p.pattern.flags = fix_flags(str, p.pattern.flags)
    831

/mnt/lovelace/software/anaconda/envs/py35/lib/python3.5/sre_parse.py in _parse_sub(source, state, nested)
    435     start = source.tell()
    436     while True:
--> 437         itemsappend(_parse(source, state))
    438         if not sourcematch("|"):
    439             break

/mnt/lovelace/software/anaconda/envs/py35/lib/python3.5/sre_parse.py in _parse(source, state)
    639             if item[0][0] in _REPEATCODES:
    640                 raise source.error("multiple repeat",
--> 641                                    source.tell() - here + len(this))
    642             if sourcematch("?"):
    643                 subpattern[-1] = (MIN_REPEAT, (min, max, item))

error: multiple repeat at position 6
作者: braid    时间: 2017-12-29 13:50
为什么要加+号?不用加的吧
作者: Charlene97    时间: 2017-12-30 19:08
braid 发表于 2017-12-29 13:50
为什么要加+号?不用加的吧

啊谢谢! 解决了. 是我把+用法搞混了. 谢谢大神!




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5