Pattern And Matcher: How to — concatenate a pattern and a string for file search

By : John V

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.

What I need to now is to create something like a wildcard or a pattern to append with say a string named ("reports"). 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()
                                               .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?


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()


Thoughts you guys? Would really appreciate it. Thanks!

By : John V


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". The toString of a Matcher object is usually not that useful.

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:

 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.

You need to replace the m + with something that makes more sense for your code.

By : chr

This video can help you solving your question :)
By: admin