正则表达式可验证至少两个字符类(字母,数字,特殊字符)

时间:2019-05-02 06:16:03

标签: java regex

我想在Java中至少匹配两个字符类,以使输入字符串仅包含小写字母[az],大写字母[AZ],数字[0-9],特殊字符[!#+,-。/ := @]。

我已经到达下面提到的正则表达式,但是它不起作用:

((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[\\d]))|((?=.*[A-Z])(?=.*[\\d]))|((?=.*[!#+,.\\=:=@-])(?=.*[\\d]))|((?=.*[!#+,.\\=:=@-])(?=.*[a-z]))|((?=.*[!#+,.\\=:=@-])(?=.*[A-Z]))

示例:

wel123 - valid
123@@! - valid
@@@aaa - valid
!!!BDC - valid
ABC123 - valid
rrirrra - invalid
1234567 - invalid
ABCFESS - invalid
@@@!!!! - invalid

谢谢。

2 个答案:

答案 0 :(得分:2)

您的正则表达式唯一缺少的是添加起始^$结束锚,并将整个正则表达式正确分组。我还从您的正则表达式中删除了不必要的分组,您可以使用此正则表达式,

^(?:(?=.*[a-z])(?=.*[A-Z])|(?=.*[a-z])(?=.*[\d])|(?=.*[A-Z])(?=.*[\d])|(?=.*[!#+,.\\=:=@-])(?=.*[\d])|(?=.*[!#+,.\\=:=@-])(?=.*[a-z])|(?=.*[!#+,.\\=:=@-])(?=.*[A-Z])).+$

Regex Demo

尽管这种方法可行,但我建议您编写一个实现此目的的代码,因为这种正则表达式对于一般人来说可能无法维护。

用于验证字符串而不是纯正则表达式的Java代码方法

以下是使用Java代码的一种方法,您可以在其中将List个模式传递给方法,以及要验证的字符串和一个数字,该数字是匹配所需的最小模式。检查此代码,

public static void main(String[] args) {
    int minRequiredMatch = 2;
    List<String> list = Arrays.asList("wel123","123@@!","@@@aaa","!!!BDC","ABC123","rrirrra","1234567","ABCFESS","@@@!!!!");

    list.forEach(x -> {
        System.out.println(x + " - " + (validateTwoCharSets(x, minRequiredMatch)?"Valid":"Invalid"));
    });
}

public static boolean validateTwoCharSets(String str, int minRequiredMatch) {
    List<Pattern> patList = new ArrayList<>();
    patList.add(Pattern.compile("[a-z]"));
    patList.add(Pattern.compile("[A-Z]"));
    patList.add(Pattern.compile("[0-9]"));
    patList.add(Pattern.compile("[!#+,-./:=@]"));

    return validateTwoCharSets(patList, str, minRequiredMatch);
}

public static boolean validateTwoCharSets(List<Pattern> patList, String str, int minRequiredMatch) {
    if (minRequiredMatch <0 || minRequiredMatch > patList.size()) {
        throw new RuntimeException("minRequiredMatch must be a positive number and not more than pattern list size.");
    }
    int matchCount = 0;

    for (Pattern p : patList) {
        Matcher m = p.matcher(str);
        if (m.find()) {
            matchCount++;
        }
        if (matchCount >= minRequiredMatch) {
            return true;
        }
    }

    return false;
}

对于给定的数据集,打印以下内容,

wel123 - Valid
123@@! - Valid
@@@aaa - Valid
!!!BDC - Valid
ABC123 - Valid
rrirrra - Invalid
1234567 - Invalid
ABCFESS - Invalid
@@@!!!! - Invalid

答案 1 :(得分:0)

我知道您想要一个正则表达式匹配您定义的集合中至少包含2个连续字符的任何字符串。

我会这样:

.*[\w\d!#+,-./:=@]{2}.*

查看完整代码:

package so20190502;

public class TwoOrMoreChars {

    public TwoOrMoreChars() {
    }

    public boolean matches(String s) {
        return s.matches(".*[\\w\\d!#+,-./:=@]{2}.*");
    }

    private void testString(String s) {
        System.out.println("String '" + s + "' does " + (matches(s)?"":"NOT") + " match.");
    }

    public static void main(String[] args) {
        TwoOrMoreChars me = new TwoOrMoreChars();
        me.testString("1");
        me.testString("1%2");
        me.testString("1%2%a%-%'");
        me.testString("1%2a-'");
    }

}

此输出:

String '1' does NOT match.
String '1%2' does NOT match.
String '1%2%a%-%'' does NOT match.
String '1%2a-'' does  match.

HTH!