今天看了一段代码,不明白怎么回事。 
C#中常用到的密码修改及正则验证代码 
public static bool IsValidPassword(string value,  
         out string errorMessage) 
      { 
         const string passwordSizeRegex = "(?=^.{6,255}$)"; ///不明白加?=在这里有什么用途,是指括号里面的内容在右端存在,但不匹配,但是在这个应用中用上有什么意思?百思不得其解...
         const string uppercaseRegex = "(?=.*[A-Z])"; 
         const string lowercaseRegex = "(?=.*[a-z])"; 
         const string punctuationRegex = @"(?=.*\d)"; 
         const string upperlowernumericRegex = "(?=.*[^A-Za-z0-9])";          bool isValid; 
         Regex regex = new Regex( 
            passwordSizeRegex + 
            "(" + punctuationRegex + uppercaseRegex + lowercaseRegex + 
               "|" + punctuationRegex + upperlowernumericRegex + lowercaseRegex + 
               "|" + upperlowernumericRegex + uppercaseRegex + lowercaseRegex + 
               "|" + punctuationRegex + uppercaseRegex + upperlowernumericRegex + 
            ")^.*");          if (value == null || value.Trim() == string.Empty) 
         { 
            isValid = false; 
            errorMessage = "Password may not be null or blank."; 
         } 
         else 
         { 
            if (regex.Match(value).Success) 
            { 
               isValid = true; 
               errorMessage = ""; 
            } 
            else 
            { 
               isValid = false; 
               errorMessage = "Password does not meet the complexity requirements."; 
            } 
         } 
         return isValid; 
      }       public void ChangePassword( 
         string oldPassword, string newPassword) 
      { 
         if (oldPassword == Password) 
         { 
            Password = newPassword; 
         } 
         else 
         { 
            throw new ArgumentException( 
               "The old password was not correct."); 
         } 
      } 
 
修改 删除 举报 引用 回复   
 将帖子提前   放进我的网摘   推荐给好友 我要提问 帖子加分 结帖去... 管理菜单 页面风格切换标准风格老版本论坛 

解决方案 »

  1.   

    Regex regex = new Regex( 
                passwordSizeRegex + 
                "(" + punctuationRegex + uppercaseRegex + lowercaseRegex + 
                   "|" + punctuationRegex + upperlowernumericRegex + lowercaseRegex + 
                   "|" + upperlowernumericRegex + uppercaseRegex + lowercaseRegex + 
                   "|" + punctuationRegex + uppercaseRegex + upperlowernumericRegex + 
                ")^.*"); 
    ???
    它想符合什么样的密码规则,看上面实在是一团浆糊。
      

  2.   

    偶太菜了,这个也不大懂,呵呵
    using System;
    using System.Text.RegularExpressions;public class Test
    {    public static void Main ()
        {        // Define a regular expression for repeated words.
            Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
              RegexOptions.Compiled | RegexOptions.IgnoreCase);        // Define a test string.        
            string text = "The the quick brown fox  fox jumped over the lazy dog dog.";
    Console.WriteLine(text);
            
            // Find matches.
            MatchCollection matches = rx.Matches(text);        // Report the number of matches found.
            Console.WriteLine("{0} matches found.", matches.Count);        // Report on each match.
            foreach (Match match in matches)
            {
                string word = match.Groups["word"].Value;
                int index = match.Index;
                Console.WriteLine("{0} repeated at position {1}", word, index);   
            }
            
        }
        
    }Microsoft (R) Visual C# 2005 编译器 版本 8.00.50727.42
    用于 Microsoft (R) Windows (R) 2005 Framework 版本 2.0.50727
    版权所有 (C) Microsoft Corporation 2001-2005。保留所有权利。The the quick brown fox  fox jumped over the lazy dog dog.
    3 matches found.
    The repeated at position 0
    fox repeated at position 20
    dog repeated at position 50C:\WINDOWS\system32>
    带颜色的都不懂,呵呵。
      

  3.   

    (?=exp) 匹配exp前面的位置 
      

  4.   

    谢谢叶子MM :)好久不见了,呵呵。\k是表示不匹配吗?
    \k是表示不匹配吗?
      

  5.   

    下表列出了用于将后向引用修饰符添加到正则表达式中的可选参数。后向引用构造 
    \number 后向引用。
    例如,(\w)\1 查找双写的单词字符。 
    \k<name> 命名后向引用。
    例如,(?<char>\w)\k<char> 查找双写的单词字符。表达式 (?<43>\w)\43 执行同样的操作。可以使用单引号替代尖括号,例如 \k'char'。 
      

  6.   

    (?=exp)也叫零宽度正预测先行断言
      

  7.   

       1. 正则其实也势利,削尖头来把钱揣; 
       2. //(指开始符号^和结尾符号$)
       3. 特殊符号认不了,弄个倒杠来引路; 
       4. //(指\. \*等特殊符号)
       5. 倒杠后面跟小w, 数字字母来表示; 
       6. //(\w跟数字字母;\d跟数字)
       7. 倒杠后面跟小d, 只有数字来表示;
       8. 倒杠后面跟小a, 报警符号嘀一声;
       9. 倒杠后面跟小b, 单词分界或退格;
      10. 倒杠后面跟小t, 制表符号很明了;
      11. 倒杠后面跟小r, 回车符号知道了;
      12. 倒杠后面跟小s, 空格符号很重要;
      13. 小写跟罢跟大写,多得实在不得了;
      14. 倒杠后面跟大W, 字母数字靠边站;
      15. 倒杠后面跟大S, 空白也就靠边站;
      16. 倒杠后面跟大D, 数字从此靠边站;
      17. 倒框后面跟大B, 不含开头和结尾;
      18.
      19. 单个字符要重复,三个符号来帮忙; 
      20. //(* + ?)
      21. 0 星加1 到无穷,问号只管0 和1; 
      22. //(*表0-n;+表1-n;?表0-1次重复)
      23. 花括号里学问多,重复操作能力强; 
      24. //({n} {n,} {n,m})
      25. 若要重复字符串,园括把它括起来; 
      26. //((abc){3} 表示字符串“abc”重复3次 )
      27. 特殊集合自定义,中括号来帮你忙;
      28. 转义符号行不通,一个一个来排队;
      29. 实在多得排不下,横杠请来帮个忙; 
      30. //([1-5])
      31. 尖头放进中括号,反义定义威力大; 
      32. //([^a]指除“a”外的任意字符 )
      33. 1竖作用可不小,两边正则互替换; 
      34. //(键盘上与“\”是同一个键)
      35. 1竖能用很多次,复杂定义很方便;
      36. 园括号,用途多;
      37. 反向引用指定组,数字排符对应它; 
      38. //(“\b(\w+)\b\s+\1\b”中的数字“1”引用前面的“(\w+)”)
      39. 支持组名自定义,问号加上尖括号; 
      40. //(“(?<Word>\w+)”中把“\w+”定义为组,组名为“Word”)
      41. 园括号,用途多,位置指定全靠它;
      42. 问号等号字符串,定位字符串前面; 
      43. //(“\b\w+(?=ing\b)”定位“ing”前面的字符串)
      44. 若要定位串后面,中间插个小于号; 
      45. //(“(?<=\bsub)\w+\b”定位“sub”后面的字符串)
      46. 问号加个惊叹号,后面跟串字符串;
      47. PHPer都知道, !是取反的意思;
      48. 后面不跟这一串,统统符合来报到; 
      49. //(“\w*d(?!og)\w*”,“dog”不符合,“do”符合)
      50. 问号小于惊叹号,后面跟串字符串;
      51. 前面不放这一串,统统符合来报到;
      52. 点号星号很贪婪,加个问号不贪婪;
      53. 加号问号有保底,至少重复一次多;
      54. 两个问号老规矩,0次1次团团转;
      55. 花括号后跟个?,贪婪变成不贪婪;
      56. 还有很多装不下,等着以后来增加。
      

  8.   

    最近一段时间比较忙,很久没有到csdn来了。
    ps:我不是MM,叫叶子好了
      

  9.   


    扩展一下就是:(?=^.{6,10}$)(?=[^\d]*\d)(?=[a-z]*[a-z])(?=[A-Z]*[A-Z])表示6-10位,至少包括一个数字,至少包含一个小写字母,至少包含一个大写字母。
      

  10.   

          (?<=    # 断言要匹配的文本的前缀
          <(\w+)>  # 查找尖括号括起来的字母或数字(即HTML/XML标签)
          )       # 前缀结束
          .*       # 匹配任意文本
          (?=      # 断言要匹配的文本的后缀
          <\/\1>   # 查找尖括号括起来的内容:前面是一个"/",后面是先前捕获的标签
          )        # 后缀结束
      

  11.   

    要引用一个命名组,使用\k或\k‘name’.
    信息来源:http://blog.sina.com.cn/s/blog_4e5cda2b01008k01.html
      

  12.   


    能够理解就好。
    以C#为例,所有的匹配都是能够在Macth.Group[0-n]中找到的,但是零宽度断言(应该叫断言而不是匹配)是在Group中找不到的,他是能够被多次检索的,被放入Group中的片断无法进行第二次检索的。
      

  13.   

    最后一个问题,不解决也没有关系了,哈哈Regex regex = new Regex( 
                passwordSizeRegex + 
                "(" + punctuationRegex + uppercaseRegex + lowercaseRegex + 
                   "|" + punctuationRegex + upperlowernumericRegex + lowercaseRegex + 
                   "|" + upperlowernumericRegex + uppercaseRegex + lowercaseRegex + 
                   "|" + punctuationRegex + uppercaseRegex + upperlowernumericRegex + 
                ")^.*"); 红色的左括号是什么意思?
      

  14.   

                passwordSizeRegex + 
                "(" + punctuationRegex + uppercaseRegex + lowercaseRegex + 
                   "|" + punctuationRegex + upperlowernumericRegex + lowercaseRegex + 
                   "|" + upperlowernumericRegex + uppercaseRegex + lowercaseRegex + 
                   "|" + punctuationRegex + uppercaseRegex + upperlowernumericRegex + 
                ")^.*"关系如下:
    *** and
    (
       ***
       or ***
       or ***
       or ***
    ) and ***去掉括号就变成
    *** 
    and ***
    or ***
    or ***
    or ***
    and ***含义当然不一样了