zoukankan      html  css  js  c++  java
  • asp.net正则表达式

    导入引用命名空间:using System.Text.RegularExpressions
                 
                //Regex类,常用方法:
                 
                //摘要:1.IsMatch(String);2.IsMatch(String, Int32);3.IsMatch(String, String);4.IsMatch(String, String, RegexOptions);5.IsMatch(String, String, RegexOptions, TimeSpan)
                //返回结果:如果正则表达式找到匹配项,则为 true;否则,为 false。
                //应用实例:验证邮政编码(5个数字)
                bool bl1 = new Regex(@"^d{5}$").IsMatch("55720");  //结果:True
    
                bool bl2 = new Regex(@"^[A-Za-z0-9]$").IsMatch("a55b720", 2);  //结果:False 
    
                bool bl3 = Regex.IsMatch("55720", @"^d{5}$");      //结果:True
                
                //Success属性
                Match match = regex.Match(input);
                while (match.Success) {
                      // Handle match here...
                      match = match.NextMatch();
                }  
    
                bool bl4 = Regex.IsMatch("55720", @"^d{5}$", RegexOptions.IgnoreCase);//结果:True
                
                //摘要:1.Match(String);2.Match(String, Int32);3.Match(String, String);4.Match(String, Int32, Int32);5.Match(String, String, RegexOptions);
                //返回结果:一个对象,包含有关匹配项的信息。
                //应用实例:取得网页标题   
                string str = Regex.Match("<title>贵源网络</title>", "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline).Groups[1].Value;  //结果:贵源网络 
                //注意:Regex.Match方法得到的Groups的索引是从1开始的,而不是从0开始的
              
    
                //摘要:1.Matches(String);2.Matches(String, Int32);3.Matches(String, String);4.Matches(String, String, RegexOptions);5.Matches(String, String, RegexOptions, TimeSpan);
                //返回结果:如果在输入字符串中发现包含任何或全部匹配,则返回匹配集合对象。
                //应用实例:遍历匹配字符串的值和项
                foreach (Match m in Regex.Matches("ababb", "a*"))
                {
                    Response.Write(String.Format("{0}-{1},", m.Value, m.Index));    //结果:a-0,-1,a-2,-3,-4,-5, 
                } 
                
    
                //摘要:
                //1.Replace(String, String);2.Replace(String, MatchEvaluator);3.Replace(String, String, Int32);4.Replace(String, String, String);5.Replace(String, String, MatchEvaluator);
                //6.Replace(String, MatchEvaluator, Int32);7.Replace(String, String, Int32, Int32);8.Replace(String, String, String, RegexOptions);9.Replace(String, String, MatchEvaluator, RegexOptions);
                //10.Replace(String, MatchEvaluator, Int32, Int32);11.Replace(String, String, String, RegexOptions, TimeSpan);12.Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan)
                //返回结果:用给定的替换字符串替换输入字符串中的匹配。
                //应用实例: 替换字符串 " ",将其用单个空格字符代替。
                string str1 = new Regex(@"s+").Replace("This   is    text", " "); //结果:This is text 
               
                //string str2 = new Regex(@"w+").Replace("four score and seven years ago", new MatchEvaluator("CapText"));
    
                string str3 = new Regex("(\w)\1").Replace("a1b2c3d4f5", "$1", 5);  //结果:a1b2c3d4f5 
    
                string str4 = Regex.Replace("This is   text", "\s+", " "); //结果:This is text 
                
                //摘要:1.Regex.Split (String);2.Regex.Split (String, Int32);3.Regex.Split (String, String);4.Regex.Split (String, Int32, Int32);5.Regex.Split (String, String, RegexOptions) 
                //返回结果:将输入字符串拆分成用正则表达式匹配分开的数组元素时,返回数组字符串。
                //应用实例:以'-'为一组进行分隔成数组
                int len1 = new Regex("(-)").Split("one-two-three").Length;  //结果:3 
    
                int len2 = new Regex(@"d+").Split("123ABCDE456FGHIJKL789MNOPQ012", 3).Length;    //结果:3 
    
                int len3 = Regex.Split("plum-pear", "-").Length;   //结果:2
    
                //结果:, ABCDE, FGHIJ789KLMNO012PQRST 
                Regex rgx = new Regex(@"d+");
                string input = "123ABCDE456FGHIJ789KLMNO012PQRST";
                Match m = rgx.Match(input);
                if (m.Success)
                {
                    string[] result = rgx.Split(input, 3, m.Index);
                    for (int ctr = 0; ctr < result.Length; ctr++)
                    {
                        Response.Write(result[ctr]);
                        if (ctr < result.Length - 1)
                            Response.Write(", ");
                    }
                }
    
                int len5 = Regex.Split("Abc1234Def5678Ghi9012Jklm", "[a-z]+", RegexOptions.IgnoreCase).Length;//结果:1
                
                //Regex类,常用属性
                 
                Compiled
                当在循环中执行许多匹配操作时使用此选项。这可以节省每一循环的分析表达式步骤。
    
                Multiline
                它与输入字符串中的行数没有关系。确切地说,它只修改 ^ 和 $ 的方式,以便匹配行开始 (BOL) 和行结尾 (EOL),而不是匹配整个输入字符串的开始和结尾。
    
                IgnoreCase
                使模式在匹配搜索字符串时忽略大小写。
    
                IgnorePatternWhitespace
                允许根据需要在模式中包括任意数量的空白区域,也支持使用 (?# 注释 #) 语法在模式中加入注释。
    
                SingleLine
                它与输入字符串中的行数没有关系。更确切地说,它将导致 .(句点)元字符匹配任意字符,而不是除 
     之外的任意字符(默认情况)。
    
  • 相关阅读:
    SQL Azure (17) SQL Azure V12
    Microsoft Azure News(5) Azure新DV2系列虚拟机上线
    Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark
    Azure PowerShell (11) 使用自定义虚拟机镜像模板,创建Azure虚拟机并绑定公网IP(VIP)和内网IP(DIP)
    Windows Azure Virtual Machine (31) 迁移Azure虚拟机
    Windows Azure Web Site (16) Azure Web Site HTTPS
    Azure China (12) 域名备案问题
    一分钟快速入门openstack
    管理员必备的Linux系统监控工具
    Keepalived+Nginx实现高可用和双主节点负载均衡
  • 原文地址:https://www.cnblogs.com/sntetwt/p/3517688.html
Copyright © 2011-2022 走看看