In Java, I have a String[]
of filepaths. I need a function to get a String[]
back, which contains all Strings that match a specified regular expression.
Does Java have a built-in function for that or will I have to do this myself?
In Java, I have a String[]
of filepaths. I need a function to get a String[]
back, which contains all Strings that match a specified regular expression.
Does Java have a built-in function for that or will I have to do this myself?
You'll have to do a loop and check string by string
public static void main(String args[]) {
String[] a = {"1", "a", "b" };
List<String> b = new ArrayList<String>();
for (int i=0; i<a.length; i++) {
if (a[i].matches("(a|b)")) { // matches uses regex
System.out.println("Match " + a[i]);
b.add(a[i]);
}
}
}
Upvote beggar:
public static void main(String args[]) {
String[] a = {"1", "a", "b" };
String [] res;
List<String> b = new ArrayList<String>();
Pattern p = Pattern.compile("(a|b)");
Matcher m;
for (int i=0; i<a.length; i++) {
m = p.matcher(a[i]);
if (m.matches()) {
System.out.println("Match " + a[i]);
b.add(a[i]);
}
}
res = (String[]) b.toArray();
}
Cleaner still:
private static String[] getMatches(String[] strings) {
Pattern p = Pattern.compile("(a|b)");
List<String> matches = new ArrayList<String>();
for (String s : strings) {
if (p.matcher(s).matches()) {
matches.add(s);
}
}
return (String[]) matches.toArray();
}
There is no such a function in Java SE to work with String array. But if you worked with file system directly then you could use
String[] files = new File("dir").list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.matches(regex);
}
});
One thing I would do is make the string array into a list object.
List<String> listFiles = Arrays.asList(strFiles);
As beder, pointed out, an iterative for loop example would probably be the easiest and safest approach to take.
The only other method using just the standard library is to make an iterator object and remove any elements from the list that don't match.
//Sudo-code
Iterator<String> iterator = listFiles.iterator();
Pattern pattern = new Pattern("foo");
while(iterator.hasNext()){
String file = iterator.next();
Matcher matcher = pattern.matcher(file);
if(!matcher.matches()){
iterator.remove();
}
}
Going outside the standard library, you might want to take a look at a few functional programming libraries. Apache-commons, Guava, and lambdaJ are the only ones that come to mind.
A matcher is just a predicate function so you could easily wrap around the higher-order function called filter. This example was written using the apache-commons syntax. How they make predicate and closure objects have some boilerplate to them. If you want something a bit more cleaner, use lambbdaJ since it's built on top of hamcrest.
public class MatchesPattern implements Predicate{
Pattern pattern;
public MatchesPattern(String strPattern){
this.pattern = new Pattern(strPattern);
}
@Override
public boolean evaluate(Object input) {
if( input instanceof String){
Matcher matcher = pattern.matcher((String)input);
return matcher.matches();
}
return false;
}
}
public void foo(String[] strFiles){
List<String> files = Arrays.asList(strFiles);
CollectionUtils.filter(files, new MatchesPattern("bar"));
}