1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

文字列チェック

Posted at
public class StringChecker {
    public static void main(String[] args) {
        String[] testInputs = {
            "Example@123",   // 大文字, 小文字, 数字, 記号 - 4種類
            "EXAMPLE123",    // 大文字, 数字 - 2種類
            "example123",    // 小文字, 数字 - 2種類
            "Example",       // 大文字, 小文字 - 2種類
            "123456",        // 数字のみ - 1種類
            "example!",      // 小文字, 記号 - 2種類
            "EXAMPLE@",      // 大文字, 記号 - 2種類
            "123!@",         // 数字, 記号 - 2種類
            "!@#$%^&*",      // 記号のみ - 1種類
            "Ab1",           // 大文字, 小文字, 数字 - 3種類
            "a!1",           // 小文字, 記号, 数字 - 3種類
            "A!@#",          // 大文字, 記号 - 2種類
        };

        for (String input : testInputs) {
            System.out.println("Input: " + input + " => " + containsAtLeastTwoTypes(input));
        }
    }

    public static boolean containsAtLeastTwoTypes(String input) {
        // 各文字種を含むかどうかの正規表現
        String letterPattern = ".*[A-Za-z].*";
        String digitPattern = ".*\\d.*";
        String specialCharPattern = ".*[!\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~].*";

        // 各正規表現に一致するかチェック
        boolean hasLetter = input.matches(letterPattern);
        boolean hasDigit = input.matches(digitPattern);
        boolean hasSpecialChar = input.matches(specialCharPattern);

        // 2種類以上の文字種が含まれているかをチェック
        int count = 0;
        if (hasLetter) count++;
        if (hasDigit) count++;
        if (hasSpecialChar) count++;

        return count >= 2;
    }
}

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?