密码正则表达式,用于特定要求 [英] Password regex for specific requirements

查看:80
本文介绍了密码正则表达式,用于特定要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为以下要求写一个正则表达式

I am to write a regex for following requirements


  1. 至少一个字符

  2. At至少一位数

  3. 长度必须为8

  4. 至少一个特殊字符(可以是任何特殊字符)

  1. At least one character
  2. At least one digit
  3. Length must be of 8
  4. At least one special character(Can be any special character)

前三个很容易,但找不到限制至少特殊字符的方法(任何可能的特殊字符,如',:* ^%>?等)。

First three are easy but couldn't find a way to restrict for at least special character(any possible special char like ',":*^%>? etc).

推荐答案

您可以通过组合前瞻来解决这些问题:

You can solve these with a combination of lookaheads:


  1. (?=。* [a-zA-Z])

  2. (?=。* \ d)

  3. 。{ 8}

  4. (?=。* [^ \ da-zA-Z])

  1. (?=.*[a-zA-Z])
  2. (?=.*\d)
  3. .{8}
  4. (?=.*[^\da-zA-Z])

最后一个只需要一个非字母和非数字,这可能是迄今为止最简单的指定你想要的方式特殊字符。

The last one just requires a non-letter and non-digit which is probably by far the easiest way of specifying that you want a somewhat "special" character.

所以最后你有

^(?=.*[a-zA-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8}$

这篇关于密码正则表达式,用于特定要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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