正則表達式(Regular Expression)分組(Group)

 基本語法html

(exp)匹配exp, 並捕獲文本到自動命名的組裏spa

(?<name>exp)  本身命名分組htm

 

static void Main(string[] args)
        {
            string pattern = "http[s]?://[\\w]+";
            pattern = "(?<=<a\\s+href=(?<link>.*?)(?=\\s+target=).*?>)(?<content>.*?)(?=</a>)";
            //pattern = "(?<link><a\\s+href=.*)";
            var reg = new Regex(pattern,RegexOptions.Multiline);
            string value = "<p>11-13<a href=/bj/11/109/4969873.html target=_blank>中介-3400元/3居 - 紫竹橋兵器大廈附近大三居(紫竹院)</a>";
            //while (!reg.IsMatch(value))
            //{
            
            //    Console.WriteLine("不匹配,請從新輸入");
            //    value = Console.ReadLine();          
            //}
            GroupCollection groups = reg.Match(value).Groups;
            var groupName = reg.GetGroupNames();
            foreach(var item in groups)
            {
                var group = item as Group;
                Console.WriteLine(group.Value);
            }
            //var g = reg.GetGroupNames();
            Console.WriteLine("匹配成功");
            Console.ReadKey();
        }blog

 

// The example displays the following output:

相關文章
相關標籤/搜索