System.Text.RegularExpressions.Regex.Replace(html, @"<.*?>", "");
string value = "<p>9:30 欢迎大家进入房间</p><p>9:30 欢迎大家进入房间</p>-----------中间一大段文字-------------<p>9:30欢迎大家进入房间</p>"; Regex reg = new Regex("<p>\\s*\\d{1,2}:\\d{1,2}\\s*[\u4e00-\u9fa5]*\\s*</p>", RegexOptions.IgnoreCase); string resultStr = ""; MatchCollection result = reg.Matches(value); for (int i = 0; i < result.Count; i ) { resultStr = result[i].Value; if (i != result.Count - 1) { resultStr = "|"; } } Response.Write(resultStr);
static void Main(string[] args)
{
string regex = @"(?is)<p[^>]*>(?><p[^>]*>(?<o>)|</p(?<-o>)|(?:(?!</?p\b).)*)*(?(o)(?!))</p>";
string Str = @"<p>9:30 欢迎大家进入房间</P>
----------大段文字----------------------
<p>9:33 我是主持人做个自我介绍</P>
----------大段文字----------------------";
Console.WriteLine(getMatchstring(regex, Str));
Console.ReadKey();
//9:30 欢迎大家进入房间|9:33 我是主持人做个自我介绍
}
#region methods
private static string getMatchstring(string reg, string src)
{
System.Text.RegularExpressions.Regex matchesRegex = new System.Text.RegularExpressions.Regex(reg, RegexOptions.IgnoreCase);
System.Text.RegularExpressions.MatchCollection matchesFound = matchesRegex.Matches(src);
System.Text.StringBuilder resultString = new System.Text.StringBuilder(64);
ArrayList myArray = new ArrayList();
myArray.Clear();
for (int i = 0; i < matchesFound.Count; i++ )
{
myArray.Add(matchesFound[i]);
}
for (int k = 0; k < myArray.Count; k++ )
{
resultString.Append(myArray[k].ToString().Remove(myArray[k].ToString().Length - 4, 4).Remove(0, 3)+ "|");
}
return resultString.ToString().TrimEnd('|');
}
执行结果:
//9:30 欢迎大家进入房间|9:33 我是主持人做个自我介绍