Follow regex


Definition

If the field match the regex expression, return true. Caution, to user parentheses use $NUMBER replacement.

Code used to check if regex match:

@Override
public boolean isOkay(Form f) {
    if (getRuleInfo().getParamsCount() != 1)
        return false;
    String regex = getRuleInfo().getParams().get(0);
    Pattern compile = Pattern.compile(regex);
    Optional<String> s = f.getString(rule.getField());
    return s.map(s1 -> compile.matcher(s1).find()).orElse(false);
}

To escape ,|: & other surround regex with parentheses.

Usage

addRule("field -> regex: (regex here)")

Tests

@Test
public final void testRuleRegexOk() throws Exception {
    UtilTest.testIt("regex: ([A-Z])", "AB", true);
    UtilTest.testIt("regex: ([A-Z])", "ABCD", true);
    UtilTest.testIt("regex: ([A-Z])", "F", true);
    UtilTest.testIt("regex: ([0-9])", "012345", true);
    UtilTest.testIt("regex: ([\\(])", "(", true);
}

@Test
public final void testRuleRegexNotOk() throws Exception {
    UtilTest.testIt("regex: ([A-Z])", "123", false);
    UtilTest.testIt("regex: ([A-Z])", "jmskcmd", false);
    UtilTest.testIt("regex: ([A-Z])", "l", false);
    UtilTest.testIt("regex: ([0-9])", "qwe", false);
    UtilTest.testIt("regex: ([\\(])", ")", false);
}

results matching ""

    No results matching ""