c# Regex筆記

來源:互聯網
上載者:User

c# Regex筆記

估計要寫幾天

看得一個不錯的正則教程包括字串教程 C#字串和Regex參考手冊.pdf

 

     正則所需要的命名空間是 using System.Text.RegularExpressions

     它包含了8個類,用得最多是的Regex;

  Regex不僅可以用來建立Regex,而且提供了許多有用的方法。

  建立一個Regex對象

  new Regex(string pattern)

     new Regex(string pattern,RegexOptions options)

     第一個參數是個字串   第二個參數正則配置的選項 有以下一些選項

  • IgnoreCase                 //是匹配忽略大小寫      預設情況區分大小寫
  • RightToLeft                 //從右至左尋找字串   預設是從左至右
  • None                          //不設定標誌  這是預設選項,就是不設定第2個參數  表示區分大小寫 從左至右
  • MultiLinc                    //指定了^和$可以匹配行的開頭和結尾,也就是說使用了換行分割,每一行能得到不同的匹配
  • SingleLine                 //規定特殊字元"."匹配任一字元,分行符號除外. 預設情況下特殊字元"."不匹配換行.(啥意思 都不匹配換行這個參數有啥用  沒看懂)

IgnoreCase的例子

      string test = "Abcccccc";
Regex reg = new Regex("abc");
Console.WriteLine(reg.IsMatch(test)); //false
Regex reg1 = new Regex("abc",RegexOptions.IgnoreCase); //不區分大小寫
Console.WriteLine(reg1.IsMatch(test));//true

RightToLeft的例子

     string test = "vvv123===456vvv";
Regex reg = new Regex("\\d+");// 123 從左至右 匹配連續數字
Console.WriteLine(reg.Match(test));
Regex reg1 = new Regex("\\d+",RegexOptions.RightToLeft);
Console.WriteLine(reg1.Match(test));// 456 從右至左 匹配連續數字

MultiLinc的例子

            StringBuilder input = new StringBuilder();
input.AppendLine("A bbbb A");
input.AppendLine("C bbbb C");

string pattern = @"^\w";
Console.WriteLine(input.ToString());
MatchCollection matchCol = Regex.Matches(input.ToString(), pattern, RegexOptions.Multiline);
foreach (Match item in matchCol)
{
Console.WriteLine("結果:{0}", item.Value);
}

 

IsMatch()

       可以用來測試字串,看他是否匹配Regex的模式.如果發現了一次匹配,就返回True.IsMatch有個靜態方法重載

Regex.IsMatch(string str,string pattern);

            string str = "abcbbbbbbbb";
string reg = @"^abc";
Console.WriteLine(Regex.IsMatch(str,reg ));//靜態重載方法
Regex pattern = new Regex("^abc");
Console.WriteLine(pattern.IsMatch(str)); //產生對象上的方法

 

Replace()
        替換字串一個匹配的模式,也有一個靜態重載方法,replace變體方法很多,我只記錄我看到的

replace(string input ,string pattern,int count,int start) 第3個參數是總共替換幾個,第4分參數是從字串的什麼位置開始替換

            string str = "123456abc";
Regex pattern = new Regex(@"\d+");
Console.WriteLine(pattern.Replace(str,"6666"));
string pattern1 = @"\d+";
Console.WriteLine(Regex.Replace(str,pattern1,"6666"));

string str1 = "asd,sss,asd,asdf,jjjj,cccc";
Regex pattern2 = new Regex(@"\w+");
Console.WriteLine(pattern2.Replace(str1, "v5v5", 2));
Console.WriteLine(pattern2.Replace(str1, "v5v5", 2,8));
// Console.WriteLine(Regex.Replace(str1, @"\w+", "v5v5", 2)); 靜態方法好像不行 會報錯 哈哈

Match()

      獲得匹配的內容(只是一次的 MatchCollection可以獲得所有的的匹配的集合)

      產生的對象上的方法   的用法

      reg.Match(string input,int start,int length)

      第一個參數是要處理的字串  第二哥參數開始的位置  第3個參數是需要匹配的長度。第2第3個參數可以不需要

      靜態方法  Regex.Match(string input , string pattern,RegexOptions options)

      第3個參數可以不要 

            string str = "vchaha vcoo vclielie vbguale vfgg vckk";
Regex pattern = new Regex(@"vc\w*");
Match matchMode = pattern.Match(str);
while (matchMode.Success)
{
Console.WriteLine(matchMode.Value);
matchMode = matchMode.NextMatch();
}
Console.WriteLine("-----------------------------------");
Match matchMode1 = Regex.Match(str, @"vc\w*");
while (matchMode1.Success)
{
Console.WriteLine(matchMode1.Value);
matchMode1 = matchMode1.NextMatch();
}

Match類的一些方法

  • NextMatch        返回下一個成功匹配的match對象
  • Result
  • Value               返回匹配的字串
  • Length             匹配的長度
  • Index               第一個匹配內容在字串中的起始位置
  • Groups             返回一個分組對象集合
  • Success            根據是否匹配成功返回ture or false

 MatchCollection()

            Regex.Matchs會返回MatchCollection類,這個集合包含了所有的Match的集合

            string input = "hahaha 123xiaodi 55nihao 66chifanlema ccc333 ccc";
Regex pattern = new Regex(@"\d+[a-z]+",RegexOptions.IgnoreCase);
MatchCollection matchsMade = pattern.Matches(input);
foreach (Match item in matchsMade)
{
Console.WriteLine(item.Value);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.