4

Possible Duplicate:
Search for a regexp in a java arraylist

Is there a way to access items stored in a List using regular expressions?

Take the following code as an example:

String jpg  = "page1.jpg";
String jpeg = "page-2.jpeg";
String png  = "page-003.png";

ArrayList<String> images = new ArrayList<String>();
images.add( jpg );
images.add( jpeg );
images.add( png );

Pattern regex = Pattern.compile( "\\D{4,5}0*1.\\w{3,4}" );
int index = images.indexOf( regex );

if( index != -1 )
    System.out.println( "Did find the string" );
else
    System.out.println( "Didn't find the string" );


The code above does not work in finding the string page1.jpg and instead returns -1.

Is there a way that this can be achieved without iterating through the List and checking whether a string matches a regex?

5
  • @Aaron Sheffey - I did see that question, but the asker specifically wanted ALL the matching items where as I just want a single item (as there will not be multiples that match the regex). I have specifically asked if there is a way to do this WITH OUT iterating which make up the answers provided in the question above. Commented Jul 29, 2011 at 12:36
  • @My Head Hurts - Why are you so opposed to iteration? indexOf uses iteration internally. Commented Jul 29, 2011 at 12:40
  • @sblundy - I'm not against the iteration - I just wanted to know if there was a way of doing it without. It was my intention to highlight that there was a difference in my question to the one Aaron linked to in his comment since he is voting to close my question. Commented Jul 29, 2011 at 12:46
  • This one is the same conceptually in terms of what you want to do, but it uses a different search mechanism: stackoverflow.com/questions/3456798/… Commented Jul 29, 2011 at 12:51
  • @Aaron Sheffey - That shows a good reason to iterate at least ;) Commented Jul 29, 2011 at 12:53

2 Answers 2

3

Not really. Short of subclassing ArrayList or using scala, groovy, etc, there isn't. indexOf uses the equals method, so you're stuck.

If you're willing to use dark arts, you could create a custom class with a equals method that uses the regex.

int index = images.indexOf(new Object() {
    public boolean equals(Object obj) {
        return regex.matches(obj.toString());
    }
});

But I'd just iterate. It's cleaner. You're co-workers and yourself in 6-months time will appreciate it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - the custom class would be a bit of an over-kill for what I am using it for so i will just iterate (and like you say, the cleaner the code, the better)
1

The code above does not work in finding the string page1.jpg and instead returns -1.

It's not supposed to work that way. The Object parameter for ArrayList.indexOf is supposed to be an object you expect to be in the collection.

What you could do is extend ArrayList and overload indexOf to support Patterns.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.