Determines if any strings in a list are in a list of phrases - Java java.lang

Java examples for java.lang:String Contain

Description

Determines if any strings in a list are in a list of phrases

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    /**//from  ww  w  . j ava2s  .  com
     * Determines if any strings in a list are in a list of phrases
     * Different than org.apache.commons.collections.CollectionUtils.containsAny() because one phrase must contain a string, not be a string
     * @param strings iterable of words that may or may not be in phrases
     * @param phrases iterable of phrases to be examined
     * @param caseSensitive true if string is matched case sensitively, false otherwise
     * @return true if any words are in any phrase, false otherwise
     */
    public static boolean stringsInPhrases(Iterable<String> strings,
            Iterable<String> phrases, Boolean caseSensitive) {

        for (String phrase : phrases) {
            for (String string : strings) {
                string = Pattern.quote(string);
                Pattern pattern = caseSensitive ? Pattern.compile(string)
                        : Pattern.compile(string, Pattern.CASE_INSENSITIVE
                                | Pattern.UNICODE_CASE);
                if (pattern.matcher(phrase).find()) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Determines if any strings in a list are in a list of phrases, case insensitively
     * Different than org.apache.commons.collections.CollectionUtils.containsAny() because one phrase must contain a string, not be a string
     * @param strings iterable of words that may or may not be in phrases
     * @param phrases iterable of phrases to be examined
     * @return true if any words are in any phrase, case insensitively, false otherwise
     */
    public static boolean stringsInPhrases(Iterable<String> strings,
            Iterable<String> phrases) {
        return stringsInPhrases(strings, phrases, false);
    }
}

Related Tutorials