About String methods of matches

Drawbacks:

Although String.matches easiest method to see if a string matches the regular expression. But it is not suitable for re-use in performance-oriented situations.

The problem is that within it as a regular expression to create a Pattern instance, but only once, and then it can be recycled garbage. Create a Pattern instance a high cost. Because of the need to compile a regular expression into a finite state machine

To improve the performance, should explicitly regular expression compiled into a Pattern example (not changing), make it a part of the class initialization, and cache it up:

public class RomanNumerals{
    private static final Pattern ROMAN=Pattern.compile(...);
    static boolean isRomanNumeral(String s){
        return ROMAN.matcher(s).matches();
    }
}

 

Guess you like

Origin www.cnblogs.com/lccsblog/p/10988928.html