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

Go To StackoverFlow.com

0

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

2012-04-03 23:52
by John V
Which one is better or any of them even better/correct - John V 2012-04-04 16:29
You're creating a Matcher object, but instead of actually using it, you are converting it to a String. That's never going to be correct - Mark B 2012-04-04 16:31
I see. Do you have any suggestions as to how I should approach this differently? I need to make a pattern that would go before "reports" - John V 2012-04-04 16:33
Well I definitely don't see any methods in the Amazon S3 API that take a Matcher object, do you? I think you're going to have to get a bigger list than you want, and then trim down the results via pattern matching in your app - Mark B 2012-04-04 18:19


1

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.

2012-04-04 00:26
by Christian Garbin
Please correct me if I misunderstood you, but from what you're saying is that with the (m + "reports") piece that I made, the matcher will look for the pattern stated on p, then concatenate it with reports, yes? If so, I believe that it is doing what I want. Although I did some minor adjustments on it. Please look at my edited post. I'd sincerely love to hear more from you - John V 2012-04-04 16:27
The m + "reports" piece will not look for any pattern. It's the same as m.toString() + "reports". The toSTring() for the Matcher object is just a string representation of the object itself, not a match. It will vary by JRE. In mine it resulted in the java.util.regex.Matcher[pattern=[a-zA-Z][0-9] region=0,4 lastmatch=] I showed above - Christian Garbin 2012-04-05 01:40
A quick look at the Amazon API seems to indicate that you can't use a regular expression as a prefix. It seems to accept a simple string only. See the example in http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/ObjectListing.html#getCommonPrefixes(). So unless you can find a positive indication that it can support a regular expression, I think you need to abandon this approach and revert to simple strings as prefixes, then have your own code to filter what you need - Christian Garbin 2012-04-05 01:49
Thanks you guys! I really appreciate the help - John V 2012-04-05 16:41
So...this is not actually an answer - Peter Davis 2015-12-31 15:31