Regex Optimization for large lists

后端 未结 4 444
逝去的感伤
逝去的感伤 2020-12-21 22:26

I am comparing two lists of strings to find possible matches. Example:

public class Tester {

    public static void main(String[] args) {

        List

        
4条回答
  •  失恋的感觉
    2020-12-21 23:14

    IMHO methods like String.matches(String) should be forbidden. Maybe you need a regex match, maybe not, but what happens here, is that you string gets compiled into an regex... again and again.

    So do yourself a favor and convert then all into regexes via Pattern.compile and reuse them.


    Looking at your ".*" + s2 + ".*", I'd bet you need no regex at all. Simply use String.contains and enjoy the speed.

提交回复
热议问题