What the is the more efficient representation that a regex string is compiled to? And how is that representation more efficient than a String?
Your question doesn't make much sense; I'm assuming you're asking about "compiled" vs "translated" RegEx which are basically the same thing anyway.
RegEx is essentially just a language for describing (to a computer) what you want to match.
Think of the RegEx string like code, the computer can't do anything with it until it's translated to something it can use first.
So "compiling" RegEx is just translating that string into machine instructions.
It is "more efficient than a String" because when you store the Pattern then you keep those instructions for matching to be re-used efficiently without having to re-translate the same RegEx string.
Eg. the below is (as I understand it) what you mean by compiled vs string representation:
public static void main(String[] args){
// using "string representation"
System.out.println("some string".matches("myRegex"));
// using "compiled representation"
Pattern myPattern = Pattern.compile("myRegex");
Matcher myMatcher1 = myPattern.matcher("some string 1");
Matcher myMatcher2 = myPattern.matcher("some string 2");
System.out.println(myMatcher1.matches());
System.out.println(myMatcher2.matches());
}
But if you look at the method used by "some string".matches("myRegex") then you'll see that it also calls Pattern.compile() to translate the RegEx instructions:
String.java
public boolean matches(String regex) {
// return Pattern.matches(regex, this);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}
So using the "string representation" still compiles the RegEx, it just doesn't cache the pattern for re-use.