自动识别 URL
识别URL超链接
识别Email
当程序已经识别出URL超链接或Email地址后,必须用<a href=...>超链接</a>对这些超链接进行替换,这样才能把这些文字显示为链接的形式。我这里把它们全部包含在函数中:
去除HTML标记
内容中提取指定标记
Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
RegexOptions.IgnoreCase|RegexOptions.Compiled);
识别Email
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
RegexOptions.IgnoreCase|RegexOptions.Compiled);
当程序已经识别出URL超链接或Email地址后,必须用<a href=...>超链接</a>对这些超链接进行替换,这样才能把这些文字显示为链接的形式。我这里把它们全部包含在函数中:
private void Button1_Click(object sender, System.EventArgs e)
{
string strContent = InputTextBox.Text;
Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = urlregex.Replace(strContent,
"<a href=\"\" target=\"_blank\"></a>");
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = emailregex.Replace(strContent, "<a href=mailto:></a>");
lbContent.Text += "<br>"+strContent;
}
{
string strContent = InputTextBox.Text;
Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = urlregex.Replace(strContent,
"<a href=\"\" target=\"_blank\"></a>");
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = emailregex.Replace(strContent, "<a href=mailto:></a>");
lbContent.Text += "<br>"+strContent;
}
去除HTML标记
ContentStr = System.Text.RegularExpressions.Regex.Replace(ContentStr,"<[^>]*>", "");
内容中提取指定标记
string content = "这里是新闻内容<IMG src=\"/images/index.jpg\" style=\"width:280px;height:160px;\">sdfsdfsdfsdf<img src='ddd'>";
Regex reg = new Regex(@"<img[^>]*>",RegexOptions.IgnoreCase|RegexOptions.Compiled|RegexOptions.RightToLeft);
foreach (Match m in reg.Matches(content))
{
content = m.Value;
}
Regex reg = new Regex(@"<img[^>]*>",RegexOptions.IgnoreCase|RegexOptions.Compiled|RegexOptions.RightToLeft);
foreach (Match m in reg.Matches(content))
{
content = m.Value;
}