Java - Write code to Return true if the passed string matches any of the passed patterns.

Requirements

Write code to Return true if the passed string matches any of the passed patterns.

Demo

//package com.book2s;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String target = "book2s.com";
        Collection filters = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(anyMatches(target, filters));
    }/*  ww w . j a v a  2s.co m*/

    /**
     * Returns true if the passed string matches any of the passed patterns.
     * False if it does not.
     * @param target
     * @param filters
     * @return
     */
    public static boolean anyMatches(String target,
            Collection<Pattern> filters) {
        if (target == null || filters == null || filters.size() < 1)
            return false;
        for (Pattern p : filters) {
            Matcher m = p.matcher(target);
            if (m.matches())
                return true;
        }
        return false;
    }
}