gpt4 book ai didi

java - 密码与正则表达式匹配

转载 作者:行者123 更新时间:2023-12-01 06:47:05 25 4
gpt4 key购买 nike

我使用 java.util.regex.Pattern 来匹配满足以下条件的密码:

  1. 至少 7 个字符
  2. 只能由字母和数字组成
  3. 至少一个字母和至少一个数字

我已经完成了 1 和 2,但我不知道如何做 3。

1 和 2 - [\\w]{7,}

有什么想法吗?

最佳答案

你可以使用这个。这基本上使用 lookahead达到第三个要求。

(?=.*\d)(?=.*[a-zA-Z])\w{7,}

或 Java 字符串

"(?=.*\\d)(?=.*[a-zA-Z])\\w{7,}"

说明

"(?=" +         // Assert that the regex below can be matched, starting at this position (positive lookahead)
"." + // Match any single character
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"\\d" + // Match a single digit 0..9
")" +
"(?=" + // Assert that the regex below can be matched, starting at this position (positive lookahead)
"." + // Match any single character
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"[a-zA-Z]" + // Match a single character present in the list below
// A character in the range between “a” and “z”
// A character in the range between “A” and “Z”
")" +
"\\w" + // Match a single character that is a “word character” (letters, digits, and underscores)
"{7,}" // Between 7 and unlimited times, as many times as possible, giving back as needed (greedy)

编辑

如果您想包含 unicode 字母支持,请使用此

(?=.*\d)(?=.*\pL)[\pL\d]{7,}

关于java - 密码与正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com