Check whether any substring of the input string matches any of the group of given regex expressions Currently only used in header row processing in StudentAttributesFactory: locateColumnIndexes Case Insensitive - Java java.lang

Java examples for java.lang:String Substring

Description

Check whether any substring of the input string matches any of the group of given regex expressions Currently only used in header row processing in StudentAttributesFactory: locateColumnIndexes Case Insensitive

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String input = "java2s.com";
        String[] regexArray = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(isAnyMatching(input, regexArray));
    }//w w w  .j a v a 2 s . c  o  m

    /**
     * Check whether any substring of the input string matches any of the group of given regex expressions
     * Currently only used in header row processing in StudentAttributesFactory: locateColumnIndexes
     * Case Insensitive
     * @param input The string to be matched
     * @param regexArray The regex repression array used for the matching
     */
    public static boolean isAnyMatching(String input, String[] regexArray) {
        for (String regex : regexArray) {
            if (isMatching(input.trim().toLowerCase(), regex)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Check whether the input string matches the regex repression
     * @param input The string to be matched
     * @param regex The regex repression used for the matching
     */
    public static boolean isMatching(String input, String regex) {
        // Important to use the CANON_EQ flag to make sure that canonical characters
        // such as ? is correctly matched regardless of single/double code point encoding
        return Pattern.compile(regex, Pattern.CANON_EQ).matcher(input)
                .matches();
    }
}

Related Tutorials