I tried to implement wildcards search in my application using some algorithms (k-gram algorithm etc.), but it was very complex.
Until i found this code, and it works perfectly .. but i don't know how it get check and get results !
Code:
public static boolean wildCardMatch(String text, String pattern)
{
return text.matches( pattern.replace("?", ".?").replace("*", ".*?") );
}
Does their anyone help me to knows how it work ? what's the idea of replace function?
What you're speaking about is called the glob pattern.
In the Java world, the glob pattern is more often translated into a regex pattern.
In your scenario, the implementation is very basic: the replace method is used to replace all occurrences of ? into the regex equivalent .?. Then all occurences of * are replaced by .*?.
So if you have the following glob pattern: abc*.def, the regex will become abc.*?.def.
When the regex is finally ready, it is used to be checked against the variable text using the method matches. This latter method accepts a regex as input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With