Does the Java regular expression library optimize for any characters. *?

I have a wrapper class for matching regular expressions. Obviously, you are compiling a regex in Patternlike this.

Pattern pattern = Pattern.compile(regex);

But suppose I used .*to indicate any number of characters. So this is basically a template.

Pattern pattern = Pattern.compile(".*");

Does the template optimize to always return true and not calculate anything? Or do I need my shell to implement this optimization? I do this because I can easily handle hundreds of thousands of regular expression operations in the process. If the regex parameter is zero, I will combine it with.*

+4
source share
1 answer

, :

.*+

Java .

Java , .*:

Java regex engine .*abc.*. , abc , . String.indexOf("abc") , . , , . , .{100}abc.*, . ? abc ( ).

Java :

  • , ( ), , , .
  • - . , \d{100} , 100 , .
  • , . , ,
  • , Pattern.compile() Pattern.matches().
  • , Matcher , reset().
  • . (X|Y|Z) , . , , , . , ; , (abcd|abef) ab(cd|ef).
  • , - , : [^a]*a [^a]*+a.
  • , , . , , .
  • # 5050507 ( regex Pattern StackOverflowError), , . .
  • (, (?:(?!something).)*) ( downvoted , ).

    , , . , , .

+3

Source: https://habr.com/ru/post/1616579/


All Articles