1. 程式人生 > >C# Regex 深入正則表示式

C# Regex 深入正則表示式

###匹配字串——從一段資料中提取自己所需要的資料資訊###

Regex regex = new Regex(正則表示式);
Regex regex = new Regex(正則表示式, RegexOptions.None | RegexOptions.IgnoreCase | RegexOptions.Multiline);
//None 無;IgnoreCase能夠在匹配是忽略大小寫;Multiline調整^和$的意義,改為匹配一行的開頭和結尾


Match m = regex.Match(內容字串);   //取一個符合條件的值
Response.Write(m.Value.ToString());  //獲得結果

Match matchs = regex.Matchs(內容字串);  //取多個符合條件的值(陣列)
foreach(Match m in atchs){         
    Response.Write(m.Value.ToString());   //遍歷獲取結果
 }
Response.Write(ms.Count);                 //陣列長度
--------------------------------------------------------------------------------
###組的概念——當獲的資料資訊是組結構時,可用它來獲取###
Regex regex = new Regex(@"(d+)/(d+)");
Match matchs = regex.Matches(@"最後比分是:19/24");

foreach(Match m in matchs)
{
  foreach(string name in regex.GetGroupNames())     //組的概念
    {
       Response.Write(("capture group"{0}"value is:"{1}"",,name,m.Groups[name].Value);
    }
}
輸出:
capture group"0"value is:"19/24"
capture group"1"value is:"19"
capture group"1"value is:"24"
附:用@"(?<score1>d+)/(?<score2>d+)"替代@"(d+)/(d+)"看看結果
--------------------------------------------------------------------------------
###替換字串###
Regex regex = new Regex(expression, option);
string result = regex.Replace(str1,str2);
//str1為原字串;str2為替換內容,它可以包含以下一些特殊字串,用來代表特別意義

$& 匹配的字串,也可以用$0
$1, $2, . . . 匹配字串中的對應組,用索引標示
${name} 匹配字串中的對應組,用名稱標示
$‘ 匹配位置之前的字串
$’ 匹配位置之後的字串
$$ 一個‘$’ 字元
$_ 輸入字串
$+ 匹配字串的所有組中,最後一個組中的資料

例1:
Regex regex = new Regex(@"/d+",RegexOptions.None);
string result = regex.Replace("fef 12/21 df 33/14 727/1", "<<$&>>");
功能:所有數字型的資料都被替換成了"<<數字>>"格式
輸出結果:fef <<12>>/<<21>> df <<33>>/<<14>> <<727>>/<<1>>
例2:
Regex regex = new Regex(@"(/d+)/(/d+)",RegexOptions.None);
string result = regex.Replace("fef 12/21 df 33/14 727/1", "$+");
功能:所有data1/data2匹配的資料,都被替換成了data2
輸出結果:fef 21 df 14 1
例三:自定義轉換公式
把”I have 200 dollars”中間的money加倍
using System.Text.RegularExpressions;
class RegularExpressions
{
   static string CapText(Match m)
    {
         string x = m.ToString();
         string result = (int.Parse(x) * 2).ToString();
         return result;
    }
   static void Main()
    {
         string text = "i have 200 dollars";
         string result = Regex.Replace(text, @"d+",new Matchuator(RegularExpressions.CapText));
         Response.Write(result);
     }
}
例三的通用函式
public static string replaceMatch(string expression, RegexOptions option, string ms,Matchuator uator)
{
   Regex regex = new Regex(expression, option);
   string result = regex.Replace(ms, uator);
   return result;
}
--------------------------------------------------------------------------------
###拆分字串###
public static void splitMatch(string expression, RegexOptions option, string ms)
{
    Regex regex = new Regex(expression, option);
    string[] result = regex.Split(ms);
    foreach(string m in result)
    {
         Response.Write("splited string is: "{0}", length: {1}",m.ToString(), m.Length);
    }
    Response.Write("splited count: {0}", result.Length);
}
程式碼簡單,不多做解釋。直接來一個smaple看看結果:
splitMatch(@"/",RegexOptions.None, "2004/4/25");
輸出:
splited string is: "2004", length: 4
splited string is: "4", length: 1
splited string is: "25", length: 2
splited count: 3