请教下大神关于正则表达式的用法 - 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
tsoingkam

请教下大神关于正则表达式的用法

  •  
  •   tsoingkam Jan 30, 2018 via iPhone 2988 views
    This topic created in 3044 days ago, the information mentioned may be changed or developed.
    import re
    text = 'ABCDE'
    f1 = re.compile('(B|C|D)')
    f2 = re.compile('(B|C|D)+')
    print(f1.findall(text))
    print(f2.findll(text))

    运行结果是:
    ['B', 'C', 'D']
    ['D']

    请教下大神们为什么第二个只返回一个 D,谢谢!!!
    5 replies    2018-01-31 15:24:36 +08:00
    TimePPT
        1
    TimePPT  
    PRO
       Jan 30, 2018   1
    因为 + 默认是贪婪( greedy )的。
    故你的 f2 = re.compile('(B|C|D)+') 中正则的实际语义是「字符串中匹配('B' | 'C'| 'D')的字符直到下一个字符不满足该表达式为止」

    所以,如果 text = 'ABCDEFABCGHBCIJCD'
    你会发现 f2.findall(text) 的结果是['D', 'C', 'C', 'D']
    chenstack
        2
    chenstack  
       Jan 30, 2018   2
    如果模式有 group,findall 返回的列表中的元素是所有 group 的内容组成的元组,(B|C|D)+的只有一个 group,其匹配内容是字串 BCD 的最后一个部分 D (结合#1 的讲解),比如把模式改为((B|C|D)+),f2.findall(text)的结果则为[('BCD', 'D')]
    Ctry
        3
    Ctry  
       Jan 31, 2018
    @chenstack
    @TimePPT
    两位 V 友,你们好。我对这边有一点疑问:为什么'(B|C|D)+'匹配到了字串 BCD,但是 findall()函数返回的是最后一部份'D'呢?为什么不是返回整个'BCD'呢?
    chenstack
        4
    chenstack  
       Jan 31, 2018
    @Ctry Help on function findall in module re:

    findall(pattern, string, flags=0)
    Return a list of all non-overlapping matches in the string.

    If one or more capturing groups are present in the pattern, return
    a list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result.

    结合我在#2 说的,如果想存粹地获取匹配的内容,可以
    import re
    text = 'ABCDE'
    f2 = re.compile('(B|C|D)+')
    print([m.group(0) for m in re.finditer(f2, text)])
    Ctry
        5
    Ctry  
       Jan 31, 2018
    @chenstack 匹配的整个内容的话我知道是'BCD',我就是不明白这个返回的组列表( list of groups )里面为什么是 D,而不是 B,或者 C。是否与贪婪匹配有关?
    About     Help     Advertise     Blog     API     FAQ     Solana     2792 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 15:40 PVG 23:40 LAX 08:40 JFK 11:40
    Do have faith in what you're doing.
    ubao msn 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