• C#正则表达式


    string RegexStr = string.Empty;
    #region 字符串匹配
     
    RegexStr = "^[0-9]+$"; //匹配字符串的开始和结束是否为0-9的数字[定位字符]
    Console.WriteLine("判断'R1123'是否为数字:{0}", Regex.IsMatch("R1123", RegexStr));
    Console.WriteLine("判断'1123'是否为数字:{0}", Regex.IsMatch("1123", RegexStr));
     
    RegexStr = @"d+"; //匹配字符串中间是否包含数字(这里没有从开始进行匹配噢,任意位子只要有一个数字即可)
    Console.WriteLine("'R1123'是否包含数字:{0}", Regex.IsMatch("R1123", RegexStr));
    Console.WriteLine("'博客园'是否包含数字:{0}", Regex.IsMatch("博客园", RegexStr));
     
    //感谢@zhoumy的提醒..已修改错误代码
    RegexStr = @"^Hello World[wW]*"; //已Hello World开头的任意字符(wW:组合可匹配任意字符)
    Console.WriteLine("'HeLLO WORLD xx hh xx'是否已Hello World开头:{0}", Regex.IsMatch("HeLLO WORLD xx hh xx", RegexStr, RegexOptions.IgnoreCase));
    Console.WriteLine("'LLO WORLD xx hh xx'是否已Hello World开头:{0}", Regex.IsMatch("LLO WORLD xx hh xx", RegexStr,RegexOptions.IgnoreCase));
    //RegexOptions.IgnoreCase:指定不区分大小写的匹配。
     
     
     
     
     
    贪婪与懒惰
    string f = "fooot";
    //贪婪匹配
    RegexStr = @"f[o]+";
    Match m1 = Regex.Match(f, RegexStr);
    Console.WriteLine("{0}贪婪匹配(匹配尽可能多的字符):{1}", f, m1.ToString());
     
    //懒惰匹配
    RegexStr = @"f[o]+?";
    Match m2 = Regex.Match(f, RegexStr);
    Console.WriteLine("{0}懒惰匹配(匹配尽可能少重复):{1}", f, m2.ToString());
     
     
     
     
     

    Replace 替换字符串

    用户在输入信息时偶尔会包含一些敏感词,这时我们需要替换这个敏感词。

     
    string PageInputStr = "靠.TMMD,今天真不爽....";
    RegexStr = @"靠|TMMD|妈的";
    Regex rep_regex = new Regex(RegexStr);
    Console.WriteLine("用户输入信息:{0}", PageInputStr);
    Console.WriteLine("页面显示信息:{0}", rep_regex.Replace(PageInputStr, "***"));
     
     

    根据数字截取字符串。

    首先,我们先看几个实际的例子:  
    1.    验证输入字符是否
    javascript: 
    var ex = "^\w+$"; 
    var re = new RegExp(ex,"i"); 
    return re.test(str); 

             VBScript 

    Dim regEx,flag,ex 

    ex = "^w+$" 

    Set regEx = New RegExp 

    regEx.IgnoreCase = True  

    regEx.Global = True  

    regEx.Pattern = ex 

    flag = regEx.Test( str ) 

                  C# 

    System.String ex = @"^w+$"; 

                  System.Text.RegularExpressions.Regex reg = new Regex( ex );                            
                  bool flag = reg.IsMatch( str ); 

    2.    验证邮件格式 

    C# 

    System.String ex = @"^w+@w+.w+$"; 

    System.Text.RegularExpressions.Regex reg = new Regex( ex ); 

    bool flag = reg.IsMatch( str ); 

    3.    更改日期的格式(用 dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式) 

    C# 

    String MDYToDMY(String input)  

       { 

          return Regex.Replace(input,  

             "\b(?\d{1,2})/(?\d{1,2})/(?\d{2,4})\b", 

             "${day}-${month}-${year}"); 

       } 

    4.    从 URL 提取协议和端口号 

    C# 

    String Extension(String url)  

       { 

          Regex r = new Regex(@"^(?w+)://[^/]+?(?:d+)?/", 

             RegexOptions.Compiled); 

          return r.Match(url).Result("${proto}${port}");  

       } 

     
  • 相关阅读:
    JS原生隐士标签扩展
    Asp.Net MVC强类型页面获取值几种方式
    Asp.Net MVC控制器获取视图传值几种方式
    vs2015 创建MVC项目
    C#后台HttpWebRequest模拟跨域Ajax请求,注册Windows服务到服务器上
    EF简介及CRUD简单DEMO
    Impala学习--代码生成(Code Generation)
    Impala源代码分析(2)-SQL解析与执行计划生成
    Impala源代码分析(3)-backend查询执行过程
    Impala源代码分析(1)-Impala架构和RPC
  • 原文地址:https://www.cnblogs.com/yunpeng521/p/6979097.html
Copyright © 2020-2023  润新知