[英]Pattern And Matcher: How to -- concatenate a pattern and a string for file search
So I'm using the AWS SDK in Java and I've created a class to download the buckets/objects that are in the S3 Server.所以我在 Java 中使用 AWS SDK 并且我创建了一个 class 来下载 S3 服务器中的存储桶/对象。
What I need to now is to create something like a wildcard or a pattern to append with say a string named ("reports").我现在需要的是为 append 创建类似通配符或模式的东西,比如一个名为(“报告”)的字符串。 The only thing I've come up with is to create a pattern-matcher variable like so:
我唯一想到的就是创建一个模式匹配器变量,如下所示:
Pattern p = Pattern.compile("[a-zA-Z][0-9]");
Matcher m = p.matcher(prePattern);
ObjectListing s3ObjectList = s3client.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix(m + "reports"));+
Can anyone please tell me if there's a better solution with what I'm trying to do or if I even did it properly?谁能告诉我我正在尝试做的事情是否有更好的解决方案,或者我是否做得正确?
Thanks!谢谢!
New code:新代码:
String bucketName = "blabla";
String prePattern = "^[a-z0-9_-]{1,30}$";
String prefixPat = " -- Insert Pattern Here -- ";
ArrayList<String> objPrefix = new ArrayList();
Pattern p = Pattern.compile(prePattern);
Matcher m = p.matcher(prefixPat);
for(int i=0; i<= objPrefix.size(); i++)
{
objPrefix.add(m + "reports");
ObjectListing s3ObjectList = s3client.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix(objPrefix.get(i)));
}
Thoughts you guys?你们觉得呢? Would really appreciate it.
真的很感激。 Thanks!
谢谢!
Don't know much about AWS, but the m + "reports"
piece of the code is invoking m.toString
and concatenating it with the literal "reports".不太了解 AWS,但是
m + "reports"
代码段正在调用m.toString
并将其与文字“报告”连接起来。 The toString of a Matcher object is usually not that useful. Matcher object 的 toString 通常没那么有用。
From this piece of code:从这段代码:
Pattern p = Pattern.compile("[a-zA-Z][0-9]");
Matcher m = p.matcher("test");
System.out.println( m + "reports" );
I get this on OS X:我在 OS X 上得到这个:
java.util.regex.Matcher[pattern=[a-zA-Z][0-9] region=0,4 lastmatch=]reports
Probably not what you want to pass on to ObjectListing
.可能不是您想传递给
ObjectListing
的内容。
You need to replace the m +
with something that makes more sense for your code.您需要将
m +
替换为对您的代码更有意义的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.