日期:2014-05-16 浏览次数:20499 次
{html}
1
{html}
2
{/html}
3
{/html}
{html}4{/html}
string validatorPattern = @"^[^\{}]*((\{(?'tag'\w+)}[^\{}]*)+((?'close-tag'\{/\k<tag>})[^\{}]*)+)*[\{}]*(?(tag)(?!))$";
string locationPattern = @"(\{(?'tag'\w+)}[^\{}]*)((\{(?'tag'\w+)}[^\{}]*)+((?'close-tag'\{/\k<tag>})[^\{}]*)+)*(?'close-tag'\{/\k<tag>})(?(tag)(?!))";
string input = @"{html}1{html}2{/html}3{/html}{html}4{/html}";
Match validator = Regex.Match(input, validatorPattern, RegexOptions.Singleline);
if (validator.Success == true)
{
var locator = new Regex(locationPattern, RegexOptions.Singleline);
foreach (Match m in locator.Matches(input))
{
Console.WriteLine("Block at {0}", m.Index);
Console.WriteLine(m.Value);
}
}
else
{
Console.WriteLine("Match failed.");
}
//提供思路 不需要正则 就可以实现简单的标签截取, 如果要是标签复杂, 把检索字符串方法换成正则的就可以了
List<string> list = new List<s