我希望下面的正则表达式匹配字符串“a34cvDrerevbf98f04389”,
\b[0-9a-zA-Z]{21}\b
但是,我发现正则表达式也可以匹配以下字符串,
000001111122222333334
#The string doesn't include a character.
AbstractAlgebraTheory
#The string doesn't include a digit.
是否有办法确保字符串中的字符和数字至少出现一次?
答案 0 :(得分:2)
使用前瞻。
\b(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{21}\b
必要时添加锚点。
^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{21}$