Reputation: 12631
I have below lines of java code.
String string1 = "SAMPLE STRING";
String string2 = "SAMPLE*";
string2 = string2.replaceAll("\\*",".*").trim().toUpperCase();
if(string1.matches(string2)){
System.out.println("true");
}else{
System.out.println("false");
}
Here i dont understand the regex meaning here. what does it mean? Can anyone please help me?
Thanks!
Upvotes: 0
Views: 146
Reputation: 422
Regex "\*" simply means '', when applied on string2 = SAMPLE by replaceAll() the string2 became SAMPLE.* and this is again a regex used by Matcher for match().
"SAMPLE.*" simply accepts everything followed by "SAMPLE"
Hence, "SAMPLE STRING" matched "SAMPLE.*".
Similarly, "SAMPLE ", "SAMPLE 123SSS", "SAMPLE" etc. will match. But, " SAMPLE STRING" doesn't matches pattern "SAMPLE.*"
Upvotes: 0
Reputation: 25874
So your regex here is "\\*"
which simply means character *
. The replaceAll()
here will replace all *
characters in string2
with .*
.
This is a common replace to have people use simplified regexes (like the ones used to match files, e.g. *.exe
) and convert it to a PCRE regex, which is the regex used in Java.
Upvotes: 2