设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 4881|回复: 3
打印 上一主题 下一主题

[正则表达式] 问题

[复制链接]

3

主题

0

好友

35

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2017-12-29 00:55:23 |只看该作者 |倒序浏览
刚刚学了正则表达式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")

回复

使用道具 举报

3

主题

0

好友

35

积分

新手上路

Rank: 1

沙发
发表于 2017-12-29 00:56:50 |只看该作者
这是错误报告, 我认真看了, 完全看不懂... \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
回复

使用道具 举报

0

主题

0

好友

114

积分

注册会员

Rank: 2

板凳
发表于 2017-12-29 13:50:26 |只看该作者
为什么要加+号?不用加的吧
回复

使用道具 举报

3

主题

0

好友

35

积分

新手上路

Rank: 1

地板
发表于 2017-12-30 19:08:45 |只看该作者
braid 发表于 2017-12-29 13:50
为什么要加+号?不用加的吧

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

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-25 17:12 , Processed in 0.025788 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部