關於c#.net Regex 學習筆記

來源:互聯網
上載者:User

一、System.Text.RegularExpression命名空間
1、 Regex類可以用來建立Regex,還提供了許多方法。
如:Regex.Replace(string input,String pattern,string replacement);
-------RegexOption枚舉
IgnoreCase 忽略大小寫。預設情況區分大小寫
RightToLeft 從右至左尋找輸入字串。
None 不設定標誌。
MiltiLine 指定^與$可以匹配行的開頭和結尾,以及字串的開頭和結尾。
SingleLine 規定特殊字元“.”匹配任一字元。分行符號除外。
例:RegexOptions.IgnoreCase
Regex.IsMatch(Mystring,"YWSM",RegexOptions.IgnoreCase |RegexOptions.RightToLeft):
-------(兩個主要的)類建構函式
Regex(string  pattern);
Regex(string  pattern , RegexOption options);
例:匹配YWSM: 
static void Main(string[] args)
{  Regex myRegex=new Regex("YWSM");
    System.Console.WriteLine(myregex.IsMatch("The first three letters of "+"the alphabet are YWSM"));   }
輸出:True如需設定區分大小寫可用
Regex myRegex=new Regex("YWSM",RegexOption.IgnoreCase);
-------IsMatch( )方法
該方法可以測試字串,看它是否匹配Regex的模式。如果發現了一次匹配,返回True,否則為False。IsMatch( )有一個靜態重載方法,使用它時可以無需顯式建立一個Regex對象。
重載形式:
public bool Regex.IsMatch(string input );
public bool Regex.IsMatch(string input,int startat);
public static bool Regex.IsMatch(string input,string pattern);
public static bool Regex.IsMatch(string input,string pattern,RegexOption options);
input: 指定了包含將檢索的文本的字串。
sartat: 指定了搜尋的起始字元位置。
pttern: 指定將匹配的樣式。
options: 匹配行為的選項。
例:string inputstring="Welcome to the ywicc,ywsmxy!";
    if ( Regex.IsMatch( inputstring,"ywicc",RegexOptions.IgnoreCase) )
          Console.WriteLine("Match Found");
    Else
          Console.WriteLine(" No Match Found");
------Replace( )方法  
用指定的字串代替一個匹配模式。
 ---基本方法有:
public static string Regex.Replace(string input,string pattern,string replacement);
public static string Regex.Replace(string input,string pattern,string replacement,RegexOption options);
例:用"AAA"替換"BBB"的所有執行個體代碼:
string inputstring="Welcome to the AAA!";
inputstring=Regex.Replace(inputstring,"BBB","AAA");
Console.WriteLine(inputstring);
----非靜態方法,可以指定替換次數的最大值以及開始下標:
Public string Replace(string input,string replacement);
Public string Replace(string input,string replacement,int count);
Public string Replace(string input,string replacement,int count,int startat);
例:使用XXX替換456之後的123,最多替換兩次,代碼如下:
string inputstring="123,456,123,123,123,789,333";
Regex regexp=new Regex("123");
Inputstring=regexp.Replace(inputstring,"XXX",2,4)
Console.WriteLine(inputstring);
輸出:123,456,XXX,XXX,123,789,333
-------Split( )方法
在每次發現匹配的位置拆分字串。返回一個字串數組。
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
class mysplit
{
    static void Main(string[ ] args)
   {
       string inputstring="123,456,789,ads";
       string[ ] splitResults;
       splitresults=Regex.Split(inputstring,",");
       StringBuilder resultsstring=new StringBuilder(32);
       foreach(string stringelement in splitresults)
      {
           resultsstring.Append(stringelement+"\n");
      }
      MessageBox.Show(resultsString.ToString( ));
    }
}    

123     <==結果
456
789
ads

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.