可以把符合條件取代 string s1 = s Rgex r = new Regex //宣告 ?變數名稱 用$ s1 = r.Rex pattern="<tr>(.*) -------------------------------------- // Declare object variable of type Regex. Regex r; // Create a Regex object and define its regular expression. r = new Regex("\s2000");
Regex r = new Regex("abc"); Match m = r.Match("123abc456"); if (m.Success) { Console.WriteLine("Found match at position " + m.Index); } ------------------ MatchCollection mc; String[] results = new String[20]; int[] matchposition = new int[20]; Regex r = new Regex("abc"); mc = r.Matches("123abc4abcd"); for (int i = 0; i < mc.Count; i++) { results[i] = mc[i].Value; matchposition[i] = mc[i].Index; }
---------------------------------------------- using System; using System.Text.RegularExpressions; public class RegexTest { public static void RunTest() { Regex r = new Regex("(a(b))c"); Match m = r.Match("abdabc"); Console.WriteLine("Number of groups found = " + m.Groups.Count); } public static void Main() { RunTest(); } }
output: Number of groups found = 3 ---------------------------------------------------------------------------------------------- using System; using System.Text.RegularExpressions; public class RegexTest { public static void RunTest() { int counter; Match m; CaptureCollection cc; GroupCollection gc; Regex r = new Regex("(Abc)+"); m = r.Match("XYZAbcAbcAbcXYZAbcAb"); gc = m.Groups; Console.WriteLine("Captured groups = " + gc.Count.ToString()); for (int i=0; i < gc.Count; i++) { cc = gc[i].Captures; counter = cc.Count; Console.WriteLine("Captures count = " + counter.ToString()); for (int ii = 0; ii < counter; ii++) { Console.WriteLine(cc[ii] + " Starts at character " + cc[ii].Index); } } } public static void Main() { RunTest(); } }
http://phorum.study-area.org/viewtopic.php?t=25450&highlight=regular+expression |