数组上的正则表达式匹配(Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14218412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex match on array (Java)
提问by Dan
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.
在 Java 中,我有一个文件String[]
路径。我需要一个函数来获取String[]
返回值,其中包含与指定正则表达式匹配的所有字符串。
Does Java have a built-in function for that or will I have to do this myself?
Java 是否有内置函数,还是我必须自己做?
回答by Evgeniy Dorofeev
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
Java SE 中没有这样的函数来处理字符串数组。但是如果你直接使用文件系统,那么你可以使用
String[] files = new File("dir").list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.matches(regex);
}
});
回答by beder
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();
}
回答by Dan
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.
正如 beder 所指出的,迭代 for 循环示例可能是最简单和最安全的方法。
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.
在标准库之外,您可能想看看一些函数式编程库。Apache-commons、Guava 和 lambdaJ 是唯一想到的。
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.
匹配器只是一个谓词函数,因此您可以轻松地环绕称为过滤器的高阶函数。此示例是使用 apache-commons 语法编写的。他们如何使谓词和闭包对象具有一些样板。如果您想要更简洁的东西,请使用 lambdaJ,因为它建立在 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"));
}