有没有更简洁的正则表达式来完成这个任务? [英] Is there a more concise regular expression to accomplish this task?

查看:34
本文介绍了有没有更简洁的正则表达式来完成这个任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于这个蹩脚的标题,我很抱歉,但我想不出更好的标题.我需要测试密码以确保以下内容:

First off, sorry for the lame title, but I couldn't think of a better one. I need to test a password to ensure the following:

密码必须至少包含以下 3 项:

Passwords must contain at least 3 of the following:

  • 大写字母
  • 小写字母
  • 数字
  • 特殊字符

这是我想出的(它有效,但我想知道是否有更好的方法来做到这一点):

Here's what I've come up with (it works, but I'm wondering if there is a better way to do this):

    Dim lowerCase As New Regex("[a-z]")
    Dim upperCase As New Regex("[A-Z]")
    Dim numbers As New Regex("\d")
    Dim special As New Regex("[\\\.\+\*\?\^\$\[\]\(\)\|\{\}\/\'\#]")

    Dim count As Int16 = 0

    If Not lowerCase.IsMatch(txtUpdatepass.Text) Then
        count += 1
    End If
    If Not upperCase.IsMatch(txtUpdatepass.Text) Then
        count += 1
    End If
    If Not numbers.IsMatch(txtUpdatepass.Text) Then
        count += 1
    End If
    If Not special.IsMatch(txtUpdatepass.Text) Then
        count += 1
    End If

如果至少有 3 个条件没有满足,我会处理.我不精通正则表达式,并且一直在网上阅读大量教程.有没有办法将所有 4 个正则表达式组合成一个?但我想这样做不会让我检查是否至少满足 3 个条件.

If at least 3 of the criteria have not been met, I handle it. I'm not well versed in regular expressions and have been reading numerous tutorials on the web. Is there a way to combine all 4 regexes into one? But I guess doing that would not allow me to check if at least 3 of the criteria are met.

顺便提一下,是否有一个站点包含所有需要在正则表达式中转义的字符(具有特殊含义的字符 - 例如 $、^ 等)的详尽列表?

On a side note, is there a site that has an exhaustive list of all characters that would need to be escaped in the regex (those that have special meaning - eg. $, ^, etc.)?

和往常一样,TIA.我无法表达我认为这个网站有多棒.

As always, TIA. I can't express enough how awesome I think this site is.

推荐答案

你拥有它的方式是最好的.您可以在一行中完成这一切,但它会非常混乱,并且根本没有帮助.

The way you have it is about as good as it can get. You could accomplish this all in one line, but it would be terribly obfuscated and wouldn't really help at all.

想想你想做什么;你想检查四个不同的标准.由于每个标准本质上都是一个比较,因此您需要单独检查每个标准,这就是您正在做的事情.

Think about what you're trying to do; you want to check for four different criteria. Since each criterion is essentially a single comparison, you'll want to check for each one individually, which is what you're doing.

这篇关于有没有更简洁的正则表达式来完成这个任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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