简体   繁体   English

模式和匹配器:如何连接模式和字符串以进行文件搜索

[英]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.

相关问题 如何让 API Gateway 验证 HTTP 查询字符串参数的模式? - How to get API Gateway to validate the pattern of HTTP query string parameters? 如何基于 BigQuery 中的字符串模式 select 列 - How do I select columns based on a string pattern in BigQuery ADF 接收器中的自定义文件名模式 - Custom file name pattern in ADF sink 如何在 Go 中实现策略模式? - How to implement Strategy Pattern in Go? 如何对模式文本进行 regexp_contains - How to regexp_contains for a pattern text Redshift regex_replace 到 select 特定的逗号分隔字符串模式 - Redshift regex_replace to select specific comma separated string pattern 如何使用 AWS CLI 仅复制 S3 存储桶中与给定字符串模式匹配的文件 - How to use AWS CLI to only copy files in S3 bucket that match a given string pattern 在 google-cloud-dataflow 中使用文件模式匹配时如何获取文件名 - How to Get Filename when using file pattern match in google-cloud-dataflow 如何将具有特定模式的 csv 文件解析为逻辑应用服务中的数组 - how to parse a csv file with a specific pattern to am array in Logic App Service .NET中Request Reply Pattern如何实现取消? - How to implement cancellation in Request Reply Pattern in .NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM