zoukankan      html  css  js  c++  java
  • 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}");  

       } 

     
  • 相关阅读:
    BZOJ5212 ZJOI2018历史(LCT)
    BZOJ5127 数据校验
    253. Meeting Rooms II
    311. Sparse Matrix Multiplication
    254. Factor Combinations
    250. Count Univalue Subtrees
    259. 3Sum Smaller
    156. Binary Tree Upside Down
    360. Sort Transformed Array
    348. Design Tic-Tac-Toe
  • 原文地址:https://www.cnblogs.com/yunpeng521/p/6979097.html
Copyright © 2011-2022 走看看