博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net正则表达式
阅读量:6942 次
发布时间:2019-06-27

本文共 4529 字,大约阅读时间需要 15 分钟。

导入引用命名空间: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("贵源网络", "([^<]*)", 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            它与输入字符串中的行数没有关系。更确切地说,它将导致 .(句点)元字符匹配任意字符,而不是除 \n 之外的任意字符(默认情况)。

 

转载地址:http://vianl.baihongyu.com/

你可能感兴趣的文章
Focal Loss 的前向与后向公式推导
查看>>
PostgreSQL远端访问
查看>>
家庭里如何搭建一个互联网可访问的服务器
查看>>
eclipse与SVN 结合(删除SVN中已经上传的问题)
查看>>
深入理解Fsync
查看>>
去掉xcode编译warning:ld: warning: directory not found for option '-L
查看>>
杭电1702--ACboy needs your help again!
查看>>
web前端开发分享-css,js进阶篇
查看>>
安大校赛/异或运算一题。
查看>>
强制回收和IDisposable.Dispose方法
查看>>
mybatis plus条件构造器
查看>>
quick sort(重复数版)
查看>>
乌班图 root权限获取
查看>>
Java内部类
查看>>
趣说Java:我是一个线程
查看>>
HDU 1498 50 years, 50 colors
查看>>
杭电 1874 畅通工程续 (求某节点到某节点的最短路径)
查看>>
PHP添加mongodb驱动的问题
查看>>
JS将秒转换为 天-时-分-秒
查看>>
CRUD
查看>>