正则表达式匹配字符串中任意顺序的至少2位数字,2个字母 [英] Regex to match at least 2 digits, 2 letters in any order in a string

查看:248
本文介绍了正则表达式匹配字符串中任意顺序的至少2位数字,2个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个模式匹配的正则表达式(用于密码),其中字符串必须在8到30个字符之间,必须至少有2个数字,至少2个字母(不区分大小写),至少1个特殊字符,没有空格。

I'm trying to create a regex to pattern match (for passwords) where the string must be between 8 and 30 characters, must have at least 2 digits, at least 2 letters (case-insensitive),at least 1 special character, and no spaces.

我有空格和特殊字符匹配工作,但我被抛出2位数字和2个字母,因为它们不需要连续。

I've got the spaces and special character matching working, but am getting thrown on the 2 digits and 2 letters because they don't need to be consecutive.

ie它应匹配 a1b2c $ ab12 $ 1aab2c $

i.e. it should match a1b2c$ or ab12$ or 1aab2c$.

这些字母有这样的东西吗?

Something like this for the letters?

(?=.*[a-zA-Z].*[a-zA-Z])  // Not sure.

此字符串有效,但仅当2个字母连续且2个数字连续时才有效。如果字母,数字,特殊字符交织在一起就会失败。

This string below works, but only if the 2 letters are consecutive and the 2 numbers are consecutive..it fails if the letters, numbers, special chars are interwoven.

(?=^.{8,30}$)((?=.*\\d)(?=.*[A-Za-z]{2})(?=.*[0-9]{2})(?=.*[!@#$%^&*?]{1})(?!.*[\\s]))^.* 


推荐答案

如果您不希望字母必须连续(?=。* [a-zA-Z]。* [a-zA-Z])是正确的方法。数字(?=。* \\d。* \\d)(?=(。* \) \d){2})

If you don't want letters to have to be consecutive (?=.*[a-zA-Z].*[a-zA-Z]) is correct approach. Same goes to digits (?=.*\\d.*\\d) or (?=(.*\\d){2}).

试试这个正则表达式

(?=^.{8,30}$)(?=(.*\\d){2})(?=(.*[A-Za-z]){2})(?=.*[!@#$%^&*?])(?!.*[\\s])^.*

这篇关于正则表达式匹配字符串中任意顺序的至少2位数字,2个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆