what is the difference between ?:, ?! and ?= in regex?

后端 未结 6 635
孤街浪徒
孤街浪徒 2020-11-29 14:34

I searched for the meaning of these expressions but couldn\'t understand the exact difference between them. This is what they say:

  • ?: Match expres
6条回答
  •  -上瘾入骨i
    2020-11-29 15:22

    This is the real difference:

    >>> re.match('a(?=b)bc', 'abc')
    
    >>> re.match('a(?:b)c', 'abc')
    
    
    # note:
    >>> re.match('a(?=b)c', 'abc')
    None
    

    If you dont care the content after "?:" or "?=", "?:" and "?=" are just the same. Both of them are ok to use.

    But if you need those content for further process(not just match the whole thing. In that case you can simply use "a(b)") You have to use "?=" instead. Cause "?:"will just through it away.

提交回复
热议问题