-
Notifications
You must be signed in to change notification settings - Fork 13
WildcardPattern
This page discusses the WildcardPattern class.
The WilcardPattern class is modeled after the Linux usage of the term. It includes 3 basic types of wildcard characters:
-
Star Wildcards: a single asterisk is used to indicate 0 or more characters. This is often used for filenames. For example:
*.csvis used to identify any filename that ends with ".csv". - Question Mark Wildcards: a question mark is used to indicate exactly one character.
-
Square Brackets Wildcard: a series of characters inside a square bracket are used to indicate exactly one character. You can either list the characters consecutively (such as
[amz]), or you can use a hyphen to indicate a range (such as[a-j]).
And then (implicitly) any other character in a WildcardPattern must be an exact match. For example if you define a pattern cat.???*, then the first four characters "cat." must exactly match an incoming String. (And after that there must be 3 or more characters.)
A simple usage might resemble:
File myFile = ...;
WildcardPattern pattern = new WildcardPattern("*.zip");
if (pattern.matches(myFile.getName())) {
// do something
}
Originally I wrote the WildcardPattern class as a simpler alternative to the java.util.regex.Pattern class.
For example: suppose I want to let users define their own file filter. Many users (or "power users") have a pretty intuitive understanding of use the asterisk as a wildcard. But in my experience it's a much smaller subset of users who want or will benefit from offering a regex-based filter.
And then once I had this in my toolkit: I started using it for myself. There's always going to be a time and place for the complexity of the Pattern class, but a lot of the time that's overkill for my needs.
Check out the new Showcase v1.03 app! It includes eyedroppers, color pickers, enhanced HTML support, misc ComponentUI's, and more.