Reluctant quantifier acting greedy

This question already has an answer here:

  • My regex is matching too much. How do I make it stop? 4 answers

  • {1} in regex is redundant since any element without specified quantifier needs to be found once. Also making it reluctant doesn't make sense since it doesn't describe range of possible repetitions (like {min,max} where adding ? would tell regex engine to make number of repetitions in that range as close to min as possible). Here {n} describes precise number of repetition so min = max = n .

    Now you should be able to solve your problem by making .+ (content between brackets) reluctant. To do so use .+? .

    So try with:

    String regex = "(((|[)(.+?)()|]))";
    
    链接地址: http://www.djcxy.com/p/13432.html

    上一篇: 理解量词

    下一篇: 不情愿量词贪婪