Regex is behaving lazy, should be greedy

后端 未结 3 1771
感动是毒
感动是毒 2021-02-20 15:12

I thought that by default my Regex would exhibit the greedy behavior that I want, but it is not in the following code:

 Regex keywords = new Reg         


        
3条回答
  •  借酒劲吻你
    2021-02-20 15:56

    According to RegularExpressions.info, regular expressions are eager. Therefore, when it goes through your piped expression, it stops on the first solid match.

    My recommendation would be to store all of your keywords in an array or list, then generate the sorted, piped expression when you need it. You would only have to do this once too as long as your keyword list doesn't change. Just store the generated expression in a singleton of some sort and return that on regex executions.

提交回复
热议问题