日期:2014-05-17  浏览次数:20403 次

求匹配多个自定义的标签的正则表达式,谢谢。
[cms:property name="username1" type="string" ishidden="false"][/cms:property]sdfsdfkkkklll[cms:property name="username2" type="string" ishidden="false"][/cms:property]klkjsldkjflskjfdljalskfj[cms:property name="username3" type="string" ishidden="false"][/cms:property]


类似上面这样的一串文本,怎么通过正则表达式取出以上的3个自定义属性呢。或者有别的办法全部获取出来嘛。,谢谢

------解决方案--------------------
首先把[和]替换成<>,然后用标准的xml解析类可得。
------解决方案--------------------
不知是否是你要的,我用到了编组。

------解决方案--------------------

string str = @"[cms:property name=""username1"" type=""string"" ishidden=""false""][/cms:property]sdfsdfkkkklll[cms:property name=""username2"" type=""string"" ishidden=""false""][/cms:property]klkjsldkjflskjfdljalskfj[cms:property name=""username3"" type=""string"" ishidden=""false""][/cms:property]";
            //循环读取
            foreach (Match m in Regex.Matches(str, @"\[(\S+)(\s*?(?<attr>[^=\[\]\s]+?=['""]?[^'""\s]+?['""])\s*?)*\]\s*?\[\/\1\]"))
            {
                Console.WriteLine("当前标签是:"+m.Groups[1].Value);
                //
                string tag = m.Groups[1].Value;
                //cms:property
                Console.WriteLine(string.Join("
------解决方案--------------------
",m.Groups["attr"].Captures.Cast<Capture>().Select(a=>a.Value)));
                List<string> list = m.Groups["attr"].Captures.Cast<Capture>().Select(a => a.Value).ToList();
                /*
                 *  [0] "name=\"username1\"" string
                [1] "type=\"string\"" string
                [2] "ishidden=\"false\"" string

   &