这是一个符合您要求的正则表达式解决方案。请记住,如果您决定稍微改变您的要求,这可能不会起作用,这忠实于这里众所周知的报价。
using System.Text.RegularExpressions;
string[] samples = {
@"Hellotoevryone<img height=""115"" width=""150"" alt="""" src=""/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg"" />Iamsogladtoseeall.",
"Testing123Hello.World",
@"Test<a href=""http://stackoverflow.com"">StackOverflow</a>",
@"Blah<a href=""http://stackoverflow.com"">StackOverflow</a>Blah<a href=""http://serverfault.com"">ServerFault</a>",
@"Test<a href=""http://serverfault.com"">Server Fault</a>", // has a space, not matched
"Stack Overflow" // has a space, not matched
};
// use these 2 lines if you don't want to use regex comments
//string pattern = @"^((?:\S(?:\<[^>]+\>)?){1,10})+$";
//Regex rx = new Regex(pattern);
// regex comments spanning multiple lines requires use of RegexOptions.IgnorePatternWhitespace
string pattern = @"^( # match line/string start, begin group
(?:\S # match (but don't capture) non-whitespace chars
(?:\<[^>]+\>)? # optionally match (doesn't capture) an html <...> tag
# to match img tags only change to (?:\<img[^>]+\>)?
){1,10} # match upto 10 chars (tags don't count per your example)
)+$ # match at least once, and match end of line/string
";
Regex rx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
foreach (string sample in samples)
{
if (rx.IsMatch(sample))
{
foreach (Match m in rx.Matches(sample))
{
// using group index 1, group 0 is the entire match which I'm not interested in
foreach (Capture c in m.Groups[1].Captures)
{
Console.WriteLine("Capture: {0} -- ({1})", c.Value, c.Value.Length);
}
}
}
else
{
Console.WriteLine("Not a match: {0}", sample);
}
Console.WriteLine();
}
使用上面的示例,这是输出(括号中的数字 = 字符串长度):
Capture: Hellotoevr -- (10)
Capture: yone<img height="115" width="150" alt="" src="/Content/Edt/image/b49768
75-8dfb-444c-8b32-cc b47b2d81e0.jpg" />Iamsog -- (116)
Capture: ladtoseeal -- (10)
Capture: l. -- (2)
Capture: Testing123 -- (10)
Capture: Hello.Worl -- (10)
Capture: d -- (1)
Capture: Test<a href="http://stackoverflow.com">StackO -- (45)
Capture: verflow</a> -- (11)
Capture: Blah<a href="http://stackoverflow.com">StackO -- (45)
Capture: verflow</a>Bla -- (14)
Capture: h<a href="http://serverfault.com">ServerFau -- (43)
Capture: lt</a> -- (6)
Not a match: Test<a href="http://serverfault.com">Server Fault</a>
Not a match: Stack Overflow