functional difference between lookarounds and non-capture group?

后端 未结 1 822
迷失自我
迷失自我 2020-12-15 11:52

I\'m trying to come up with an example where positive look-around works but non-capture groups won\'t work, to further understand their usages. The examples I\"m coming up

相关标签:
1条回答
  • 2020-12-15 12:22

    The fundamental difference is the fact, that non-capturing groups still consume the part of the string they match, thus moving the cursor forward.

    One example where this makes a fundamental difference is when you try to match certain strings, that are surrounded by certain boundaries and these boundaries can overlap. Sample task:

    Match all as from a given string, that are surrounded by bs - the given string is bababaca. There should be two matches, at positions 2 and 4.

    Using lookarounds this is rather easy, you can use b(a)(?=b) or (?<=b)a(?=b) and match them. But (?:b)a(?:b) won't work - the first match will also consume the b at position 3, that is needed as boundary for the second match. (note: the non-capturing group isn't actually needed here)

    Another rather prominent sample are password validations - check that the password contains uppercase, lowercase letters, numbers, whatever - you can use a bunch of alternations to match these - but lookaheads come in way easier:

    (?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!?.])
    

    vs

    (?:.*[a-z].*[A-Z].*[0-9].*[!?.])|(?:.*[A-Z][a-z].*[0-9].*[!?.])|(?:.*[0-9].*[a-z].*[A-Z].*[!?.])|(?:.*[!?.].*[a-z].*[A-Z].*[0-9])|(?:.*[A-Z][a-z].*[!?.].*[0-9])|...
    
    0 讨论(0)
提交回复
热议问题