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

后端 未结 6 639
孤街浪徒
孤街浪徒 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条回答
  •  抹茶落季
    2020-11-29 15:16

    The difference between ?= and ?! is that the former requires the given expression to match and the latter requires it to not match. For example a(?=b) will match the "a" in "ab", but not the "a" in "ac". Whereas a(?!b) will match the "a" in "ac", but not the "a" in "ab".

    The difference between ?: and ?= is that ?= excludes the expression from the entire match while ?: just doesn't create a capturing group. So for example a(?:b) will match the "ab" in "abc", while a(?=b) will only match the "a" in "abc". a(b) would match the "ab" in "abc" and create a capture containing the "b".

提交回复
热议问题