求匹配多个自定义的标签的正则表达式,谢谢。
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
*/
}
string pattern = "(?<=\\[cms:property).*?(?=\\]\\[/cms:property\\])";
MatchCollection mc = Regex.Matches("字符串内容", pattern);
foreach(Match m in mc)
{
string pt = m.ToString();
// do something
}