Does '[ab]+' equal '(a|b)+' in python re module?

前端 未结 1 1123
-上瘾入骨i
-上瘾入骨i 2021-01-11 17:45

I think pat1 = \'[ab]\' and pat2 = \'a|b\' have the same function in Python(python2.7, windows) \'re\' module as a regular expression pattern. But I am confused with \'[ab]+

相关标签:
1条回答
  • 2021-01-11 18:39

    You have a capturing group in the first pattern.

    According to the docs,

    re.split()
    ... If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. ...

    Try making the group non-capturing and see if you get what you expect:

    pat1 = '(?:a|b)+'
    
    0 讨论(0)
提交回复
热议问题