I'm writing a simple debugging program that takes as input simple strings that can contain stars to indicate a wildcard match-any
*.wav // matches <anything>.wav
(*, a) // matches (<anything>, a)
I thought I would simply take that pattern, escape any regular expression special characters in it, then replace any \\*
back to .*
. And then use a regular expression matcher.
But I can't find any Java function to escape a regular expression. The best match I could find is Pattern.quote
, which however just puts \Q
and \E
at the begin and end of the string.
Is there anything in Java that allows you to simply do that wildcard matching without you having to implement the algorithm from scratch?
A wildcard pattern is a series of characters that are matched against incoming character strings. You can use these patterns when you define pattern matching criteria. Matching is done strictly from left to right, one character or basic wildcard pattern at a time.
A wildcard is a symbol that takes the place of an unknown character or set of characters. Commonly used wildcards are the asterisk ( * ) and the question mark ( ? ). Depending on the software or the search engine you are using, other wildcard characters may be defined.
Wildcards are special symbols that can be used to match characters in string values. TestComplete supports two standard wildcards: the asterisk (*) and the question mark (?). The asterisk wildcard corresponds to a string of any length (including an empty string). The question mark corresponds to any single character.
Just escape everything - no harm will come of it.
String input = "*.wav";
String regex = ("\\Q" + input + "\\E").replace("*", "\\E.*\\Q");
System.out.println(regex); // \Q\E.*\Q.wav\E
System.out.println("abcd.wav".matches(regex)); // true
Or you can use character classes:
String input = "*.wav";
String regex = input.replaceAll(".", "[$0]").replace("[*]", ".*");
System.out.println(regex); // .*[.][w][a][v]
System.out.println("abcd.wav".matches(regex)); // true
It's easier to "escape" the characters by putting them in a character class, as almost all characters lose any special meaning when in a character class. Unless you're expecting weird file names, this will work.
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