密码格式的正则表达式 [英] Regular Expression for Password Format

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

问题描述

我有以下密码规则.我需要为此使用正则表达式.

请遵循以下密码规则:
a.最小密码长度为8
b.最小数字字符为1
c.最小字母字符为2
d.任何特殊角色都可以访问

Theigi Win

解决方案

尝试一下:

  char  [] cArray = textBox1.Text.ToCharArray();
 int  length = cArray.Length;
布尔 hasDigit = cArray.Any(c = >  > char  .IsDigit(c));
 int  AlphabetCount = cArray.Count(c = >   char  .IsLetter(c));
 int  specialCharCount = cArray.Count(c = > !char.IsLetterOrDigit(c));  pre> 

希望这会有所帮助!


尝试一下:-


 私有  void  button1_Click(对象发​​件人,EventArgs e)
        {
            StringBuilder sb =  StringBuilder();
            字符串 str = textBox1.Text.Trim();
             int  noOfChr =  0 ;
             int  noOfNo =  0 ;
             for ( int  i =  0 ; i <  str.Length; i ++)
            {
                如果(字符 .IsLetter(str [i]))
                {
                    sb.Append(str [i]);
                    noOfChr = noOfChr +  1 ;

                }
                其他 如果( char  .IsDigit(str [一世]))
                {
                    sb.Append(str [i]);
                    noOfNo = noOfNo +  1 ;

                }
            }
            如果(noOfNo <   1 ){ MessageBox.Show(" ); 返回; }
            如果(noOfChr <   2 ){ MessageBox.Show(" ); 返回; }


        } 


我认为这里的正则表达式不是最好的工具,因为您的要求没有任何正则模式;所有列出的功能可以所有可能的组合进行混合.

这个问题很容易解决.将您的要求视为伪代码,并将"a"-"d"项作为单独的谓词.通过对每个功能进行计数,将伪代码转换为真实代码.这样,您甚至可以返回一个字符串,逐项向用户报告所有有关用户密码候选者的不利条件.有了正则表达式,这个好功能将完全不可能.

编写此简单的代码,如果遇到任何问题,请提问.

祝你好运,
—SA


I have the following password rules. I need regular expression for this.

Please follow the following password rules:
a.Minimum Password Length is 8
b.Minimum Numeric Character is 1
c.Minimum Alphabet Character is 2
d.Any Special Characters can access

Theigi Win

解决方案

Try this:

char[] cArray = textBox1.Text.ToCharArray();
int length = cArray.Length;
bool hasDigit = cArray.Any(c => char.IsDigit(c));
int alphabetCount = cArray.Count(c => char.IsLetter(c));
int specialCharCount = cArray.Count(c => !char.IsLetterOrDigit(c));



Hope this helps!


try this :-


private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string str = textBox1.Text.Trim();
            int noOfChr = 0;
            int noOfNo = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsLetter(str[i]))
                {
                    sb.Append(str[i]);
                    noOfChr = noOfChr + 1;

                }
                else if (char.IsDigit(str[i]))
                {
                    sb.Append(str[i]);
                    noOfNo = noOfNo + 1;

                }
            }
            if (noOfNo < 1) { MessageBox.Show("Enter minimum 1 Digit !!"); return; }
            if (noOfChr < 2) { MessageBox.Show("Enter minimum 2 Alphabet !!"); return; }


        }


I think Regular Expression is not the best tool here, because your requirements don''t present any regular pattern; all the listed features can be mixed in all possible combinations.

The problem is easy-to-solve. Consider your requirements as pseudo-code and your ''a''-''d'' items as separate predicates. Make your pseudo-code into real code by counting each feature. In this way, you could even return a string reporting all disadvantaged about user''s password candidate to the user item by item. With Regular Expression this nice feature would be completely impossible.

Write this simple code and ask a question if you face any problems.

Good luck,
—SA


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

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