src/main/java/utils/Common.java
/**
     * <p>Match keywords with a given string. Case insensitive.</p>
     * <p>Type OR, AND or NOT for boolean operations. White space is treated as OR.</p>
     * <p>Escape white space in the keyword with '\s'</p>
     * <p>Use curly brackets {} to group keywords. Nested groups are supported.</p>
     * <br>Keyword example:<br>
     *     Motherboard AND NOT {MSI Intel DDR2}
     *
     * @param text String to be searched for keywords
     * @param keywords a string of keywords
     * @return : true if keyword pattern match
     */
Match keywords with a given string. Case insensitive.
Type OR, AND or NOT for boolean operations. White space is treated as OR.
Escape white space in the keyword with '\s'
Use curly brackets {} to group keywords. Nested groups are supported.
Keyword example:
Motherboard AND NOT {MSI Intel DDR2}

@param text String to be searched for keywords
@param keywords a string of keywords
@return : true if keyword pattern match
Match keywords with a given string . Case insensitive . Type OR AND or NOT for boolean operations . White space is treated as OR . Escape white space in the keyword with ' \ s ' Use curly brackets {} to group keywords . Nested groups are supported . Keyword example : Motherboard AND NOT { MSI Intel DDR2 } @param text String to be searched for keywords @param keywords a string of keywords @return : true if keyword pattern match
public static boolean matchKeywordsToString(String text, String keywords) {
        text = text.toLowerCase();
        String rUnacceptable = "((^|\\s)(OR|AND|NOT)$|(^|\\s)(AND\\sOR|NOT\\sNOT|NOT\\sAND|NOT\\sOR|AND\\sAND|OR\\sOR)(\\s|$)|^(OR|AND)\\s)";
        String rEmptyGroup = "(\\{\\s}\\sAND\\s|\\sAND\\s\\{\\s}|(\\s|^)NOT\\s\\{\\s})";
        Pattern pUnacceptable = Pattern.compile(rUnacceptable);
        Pattern pEmptyGroup = Pattern.compile(rEmptyGroup);
        Pattern pCurlyBracketOpened = Pattern.compile("\\{");
        Pattern pCurlyBracketClosed = Pattern.compile("}");
        Matcher m;
        int countCurlyBracketOpened = 0;
        int countCurlyBracketClosed = 0;
        boolean result;
        keywords = keywords.trim().replaceAll("\\s+", " ");
        if (keywords.equals("")) return true;
        m = pUnacceptable.matcher(keywords);
        if (m.find()) throw new SyntaxException("ERROR! Could not parse keywords. Syntax error inside keywords string");
        m = pCurlyBracketOpened.matcher(keywords);
        while (m.find()) countCurlyBracketOpened++;
        m = pCurlyBracketClosed.matcher(keywords);
        while (m.find()) countCurlyBracketClosed++;
        if (countCurlyBracketOpened != countCurlyBracketClosed)
            throw new SyntaxException("ERROR! Group is missing one or more curly brackets");
        keywords = keywords.replaceAll("\\sOR\\s", " ");
        keywords = keywords.replaceAll("\\{", " { ").replaceAll("}", " } ").trim().replaceAll("\\s+", " ");
        m = pEmptyGroup.matcher(keywords);
        while (m.find()) throw new SyntaxException("Empty group before AND or after NOT is not allowed");
        keywords = keywords.replaceAll("\\{\\s}", "").trim().replaceAll("\\s+", " ");
        KeywordCollection k = new KeywordCollection(keywords);
        while (processRecursiveAllKeywords(text, k)) ;
        result = k.getResult();
        return result;
    }
public static boolean matchKeywordsToString ( String text , String keywords ) { text = text . toLowerCase ( ) ; String rUnacceptable = "((^|\\s)(OR|AND|NOT)$|(^|\\s)(AND\\sOR|NOT\\sNOT|NOT\\sAND|NOT\\sOR|AND\\sAND|OR\\sOR)(\\s|$)|^(OR|AND)\\s)" ; String rEmptyGroup = "(\\{\\s}\\sAND\\s|\\sAND\\s\\{\\s}|(\\s|^)NOT\\s\\{\\s})" ; Pattern pUnacceptable = Pattern . compile ( rUnacceptable ) ; Pattern pEmptyGroup = Pattern . compile ( rEmptyGroup ) ; Pattern pCurlyBracketOpened = Pattern . compile ( "\\{" ) ; Pattern pCurlyBracketClosed = Pattern . compile ( "}" ) ; Matcher m ; int countCurlyBracketOpened = 0 ; int countCurlyBracketClosed = 0 ; boolean result ; keywords = keywords . trim ( ) . replaceAll ( "\\s+" , " " ) ; if ( keywords . equals ( "" ) ) return true ; m = pUnacceptable . matcher ( keywords ) ; if ( m . find ( ) ) throw new SyntaxException ( "ERROR! Could not parse keywords. Syntax error inside keywords string" ) ; m = pCurlyBracketOpened . matcher ( keywords ) ; while ( m . find ( ) ) countCurlyBracketOpened ++ ; m = pCurlyBracketClosed . matcher ( keywords ) ; while ( m . find ( ) ) countCurlyBracketClosed ++ ; if ( countCurlyBracketOpened != countCurlyBracketClosed ) throw new SyntaxException ( "ERROR! Group is missing one or more curly brackets" ) ; keywords = keywords . replaceAll ( "\\sOR\\s" , " " ) ; keywords = keywords . replaceAll ( "\\{" , " { " ) . replaceAll ( "}" , " } " ) . trim ( ) . replaceAll ( "\\s+" , " " ) ; m = pEmptyGroup . matcher ( keywords ) ; while ( m . find ( ) ) throw new SyntaxException ( "Empty group before AND or after NOT is not allowed" ) ; keywords = keywords . replaceAll ( "\\{\\s}" , "" ) . trim ( ) . replaceAll ( "\\s+" , " " ) ; KeywordCollection k = new KeywordCollection ( keywords ) ; while ( processRecursiveAllKeywords ( text , k ) ) ; result = k . getResult ( ) ; return result ; }
<p>Match keywords with a given string.
<p > Match keywords with a given string .
// Replace all multiple spaces with one space and trim spaces on both ends and replace all OR with space // If string is empty return true // Check for error in the syntax of keywords // Test that all groups are enclosed // Replace all spaces between words with OR // Put a space around bracket to identify the group. Trim and replace double spaces.