導入引用命名空間:using System.Text.RegularExpressions
//Regex類,常用方法:
//摘要:1.IsMatch(String);2.IsMatch(String, Int32);3.IsMatch(String, String);4.IsMatch(String, String, RegexOptions);5.IsMatch(String, String, RegexOptions, TimeSpan)
//返回結果:如果正則表達式找到匹配項,則為 true;否則,為 false。
//應用實例:驗證郵政編碼(5個數字)
bool bl1 = new Regex(@"^\d{5}$").IsMatch("55720"); //結果:True
bool bl2 = new Regex(@"^[A-Za-z0-9]$").IsMatch("a55b720", 2); //結果:False
bool bl3 = Regex.IsMatch("55720", @"^\d{5}$"); //結果:True
//Success屬性
Match match = regex.Match(input);
while (match.Success) {
// Handle match here...
match = match.NextMatch();
}
bool bl4 = Regex.IsMatch("55720", @"^\d{5}$", RegexOptions.IgnoreCase);//結果:True
//摘要:1.Match(String);2.Match(String, Int32);3.Match(String, String);4.Match(String, Int32, Int32);5.Match(String, String, RegexOptions);
//返回結果:一個對象,包含有關匹配項的信息。
//應用實例:取得網頁標題
string str = Regex.Match("<title>貴源網絡</title>", "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline).Groups[1].Value; //結果:貴源網絡
//注意:Regex.Match方法得到的Groups的索引是從1開始的,而不是從0開始的
//摘要:1.Matches(String);2.Matches(String, Int32);3.Matches(String, String);4.Matches(String, String, RegexOptions);5.Matches(String, String, RegexOptions, TimeSpan);
//返回結果:如果在輸入字符串中發現包含任何或全部匹配,則返回匹配集合對象。
//應用實例:遍歷匹配字符串的值和項
foreach (Match m in Regex.Matches("ababb", "a*"))
{
Response.Write(String.Format("{0}-{1},", m.Value, m.Index)); //結果:a-0,-1,a-2,-3,-4,-5,
}
//摘要:
//1.Replace(String, String);2.Replace(String, MatchEvaluator);3.Replace(String, String, Int32);4.Replace(String, String, String);5.Replace(String, String, MatchEvaluator);
//6.Replace(String, MatchEvaluator, Int32);7.Replace(String, String, Int32, Int32);8.Replace(String, String, String, RegexOptions);9.Replace(String, String, MatchEvaluator, RegexOptions);
//10.Replace(String, MatchEvaluator, Int32, Int32);11.Replace(String, String, String, RegexOptions, TimeSpan);12.Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan)
//返回結果:用給定的替換字符串替換輸入字符串中的匹配。
//應用實例: 替換字符串 " ",將其用單個空格字符代替。
string str1 = new Regex(@"\s+").Replace("This is text", " "); //結果:This is text
//string str2 = new Regex(@"\w+").Replace("four score and seven years ago", new MatchEvaluator("CapText"));
string str3 = new Regex("(\\w)\\1").Replace("a1b2c3d4f5", "$1", 5); //結果:a1b2c3d4f5
string str4 = Regex.Replace("This is text", "\\s+", " "); //結果:This is text
//摘要:1.Regex.Split (String);2.Regex.Split (String, Int32);3.Regex.Split (String, String);4.Regex.Split (String, Int32, Int32);5.Regex.Split (String, String, RegexOptions)
//返回結果:將輸入字符串拆分成用正則表達式匹配分開的數組元素時,返回數組字符串。
//應用實例:以'-'為一組進行分隔成數組
int len1 = new Regex("(-)").Split("one-two-three").Length; //結果:3
int len2 = new Regex(@"\d+").Split("123ABCDE456FGHIJKL789MNOPQ012", 3).Length; //結果:3
int len3 = Regex.Split("plum-pear", "-").Length; //結果:2
//結果:, ABCDE, FGHIJ789KLMNO012PQRST
Regex rgx = new Regex(@"\d+");
string input = "123ABCDE456FGHIJ789KLMNO012PQRST";
Match m = rgx.Match(input);
if (m.Success)
{
string[] result = rgx.Split(input, 3, m.Index);
for (int ctr = 0; ctr < result.Length; ctr++)
{
Response.Write(result[ctr]);
if (ctr < result.Length - 1)
Response.Write(", ");
}
}
int len5 = Regex.Split("Abc1234Def5678Ghi9012Jklm", "[a-z]+", RegexOptions.IgnoreCase).Length;//結果:1
//Regex類,常用屬性
Compiled
當在循環中執行許多匹配操作時使用此選項。這可以節省每一循環的分析表達式步驟。
Multiline
它與輸入字符串中的行數沒有關系。確切地說,它只修改 ^ 和 $ 的方式,以便匹配行開始 (BOL) 和行結尾 (EOL),而不是匹配整個輸入字符串的開始和結尾。
IgnoreCase
使模式在匹配搜索字符串時忽略大小寫。
IgnorePatternWhitespace
允許根據需要在模式中包括任意數量的空白區域,也支持使用 (?# 注釋 #) 語法在模式中加入注釋。
SingleLine
它與輸入字符串中的行數沒有關系。更確切地說,它將導致 .(句點)元字符匹配任意字符,而不是除 \n 之外的任意字符(默認情況)。