【问题标题】:Password checker - check for 2 identical characters adjacent with one another密码检查器 - 检查 2 个彼此相邻的相同字符
【发布时间】:2018-03-23 10:30:44
【问题描述】:

我正在做一个程序来确定 vb.net 中密码的强度

“强”密码的特点如下:

1) 长度必须至少为 8 个字符

2) 必须是字母、数字和符号的组合

3) 必须至少有 2 个大写字母和

4) 不能有 2 个相同的字符彼此相邻。

示例: b@LLp3n = 弱

P@ssw0rd = 弱

k3Yb0Ard! = 强

C0MPUT3R = 弱

S+@Rcr4ft = 强

问题: 我担心的是检查两个彼此相邻的相同字符的正确代码。

代码如下:

Public Class Form1
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim password As String = txtInput.Text
    Dim numUpper As Integer = 2
    Dim passLength As Integer = 8
    Dim specialLength As Integer = 1


    Dim upper As New Text.RegularExpressions.Regex("[A-Z]")
    Dim specialChar As New Text.RegularExpressions.Regex("[^a-zA-Z0-9]")


    If password.Length < passLength Then
        MessageBox.Show("The Password Is Weak-")
    ElseIf upper.Matches(password).Count < numUpper Then
        MessageBox.Show("The Password Is Weak!")
    ElseIf specialChar.Matches(password).Count < specialLength Then
        MessageBox.Show("The Password Is Weak+")
    Else
        MessageBox.Show("The Password Is Strong")
    End If
End Sub
End Class

我希望有人可以帮助我解决这个问题。

【问题讨论】:

  • If Regex.IsMatch(password,"(.)\1")....
  • 非常感谢@WiktorStribiżew :)
  • 看我的回答,我添加了更多细节。
  • @WiktorStribiżew。它真的对我有用,我很欣赏 RegEx 的参考。我将在即将进行的与密码验证相关的项目中使用它。

标签: regex vb.net passwords


【解决方案1】:

您可以在您的方法中添加另一个If 条件:

If Regex.IsMatch(password,"(.)\1") Then
    MessageBox.Show("There are identical consecutive chars!")
End If

(.)\1 模式将匹配并捕获除换行符以外的任何字符(使用 (.)),并且对第 1 组值的 \1 反向引用将匹配在第 1 组中捕获的相同字符。请参阅the regex demo

您可以进一步增强它。比如说,您允许两个相同的连续字符,但您不想允许 3 个相同的字符条纹。然后使用(.)\1{2},这将需要在第 1 组中捕获 2 次相同的字符。

【讨论】:

  • 非常感谢@Wiktor 我现在明白了。我非常感谢正则表达式参考的参考。我很快就会使用它。
猜你喜欢
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 2018-03-31
  • 2011-09-08
相关资源
最近更新 更多