I have following string as a glob rule:
**/*.txt
And test data:
/foo/bar.txt
/foo/buz.jpg
/foo/oof/text.txt
Is it possible to use glob rule (without converting glob to regex) to match test data and return valud entries ?
One requirement: Java 1.6
If you have Java 7 can use FileSystem.getPathMatcher
:
final PathMatcher matcher = FileSystem.getPathMatcher("glob:**/*.txt");
This will require converting your strings into instances of Path
:
final Path myPath = Paths.get("/foo/bar.txt");
For earlier versions of Java you might get some mileage out of Apache Commons' WildcardFileFilter
. You could also try and steal some code from Spring's AntPathMatcher
- that's very close to the glob-to-regex approach though.
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