Regex possesive quantifier

前端 未结 1 2023
臣服心动
臣服心动 2021-01-13 20:44

I was answering this question, here is a direct link to my answer.

You will notice that I used the pattern:

(\\\\?)?&?(TXT\\\\{[^}]++})(&)?
<         


        
1条回答
  •  孤城傲影
    2021-01-13 21:01

    I guess, this is to do with how possessive quantifiers work. First they work like greedy quantifier. In the sense, they will try to match as much as they can. But unlike greedy quantifier, once they match something, they won't give up the match after backtracking.

    So, taking your regex:

    "(\\?)?+&?(TXT\\{[^}]++})(&)?"
    

    It first finds the ? before username, so it matches that and stores it in group 1. Then it finds that the next character & doesn't match the u of username. So it backtracks, and stops at ?. Since that was matched as a possessive quantifier, they don't loose the match.

    Now, it proceeds further. At this point, group 1 still contains the ?. Now it matches the part:

    &TXT{UE-IP,UE-Username,UE-Password}&
    

    Where since ? is optional, it is not matched. But it doesn't replace anything in group 1.

    That means, you're getting the ? from the group 1 that was matched first time.


    This seems to be a bug in Java regex engine, as in Perl, that group is coming as undefined. Here's the fiddle.

    0 讨论(0)
提交回复
热议问题