请教一个 re 正则的问题 - V2EX
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
plko345
V2EX    Python

请教一个 re 正则的问题

  •  
  •   plko345 2021-05-09 12:37:32 +08:00 2413 次点击
    这是一个创建于 1618 天前的主题,其中的信息可能已经有所发展或是发生改变。

    匹配 数字三连....

    import re s = 'abc 456 def' re.search('\d\d\d', s) # 成功 re.search('\d{3}', s) # None re.search('[0-9]{3}', s) # 成功 

    为什么 \d{3} 不行, 我在在线正则测试的工具网站上试过 \d{3} 并没有错, google 也没找到有说明的, 可能是关键字不对

    9 条回复    2021-05-10 09:24:46 +08:00
    oldshensheep
        1
    oldshensheep  
       2021-05-09 12:44:23 +08:00
    Trim21
        2
    Trim21  
       2021-05-09 12:47:21 +08:00 via Android
    试了试第二个能正确匹配
    cherbim
        3
    cherbim  
       2021-05-09 13:06:38 +08:00 via iPhone
    我测试第二个成功啊
    ClericPy
        4
    ClericPy  
       2021-05-09 13:52:37 +08:00
    python 3.7 3.8 3.9 2.7 都能正确匹配

    要不你试试 r'\d{3}'
    iptables
        5
    iptables  
       2021-05-09 13:56:46 +08:00
    我的测试结果

    $ python3.9
    Python 3.9.5 (default, May 3 2021, 19:12:05)
    [Clang 12.0.5 (clang-1205.0.22.9)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> import re
    >>>
    >>> s = 'abc 456 def'
    >>>
    >>> re.search('\d\d\d', s)
    <re.Match object; span=(4, 7), match='456'>
    >>> re.search('\d{3}', s)
    <re.Match object; span=(4, 7), match='456'>
    >>> re.search('[0-9]{3}', s)
    <re.Match object; span=(4, 7), match='456'>
    >>>
    >>> re.search(r'\d\d\d', s)
    <re.Match object; span=(4, 7), match='456'>
    >>> re.search(r'\d{3}', s)
    <re.Match object; span=(4, 7), match='456'>
    >>> re.search(r'[0-9]{3}', s)
    <re.Match object; span=(4, 7), match='456'>
    >>>
    plko345
        6
    plko345  
    OP
       2021-05-09 14:00:57 +08:00
    @Trim21
    @cherbim
    @ClericPy
    @iptables

    谢谢各位, 尴尬了, 我再找找是不是我哪里搞错了
    crystom
        7
    crystom  
       2021-05-09 14:25:21 +08:00   3
    python3 的\d 包括各种 unicode 数字,不光是 0-9
    imn1
        8
    imn1  
       2021-05-09 15:05:31 +08:00
    @crystom #7
    哇靠,还真不知这个,测试了一下,全角数字也能匹配,不会罗马数字也行吧?
    yunyuyuan
        9
    yunyuyuan  
       2021-05-10 09:24:46 +08:00
    @crystom 涨见识了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3106 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 12:36 PVG 20:36 LAX 05:36 JFK 08:36
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86