zoukankan
html css js c++ java
将地址转换为链接的正则表达式(regex url href)
将文章内容中没有链接的地址转换为链接。
代码如下:
public
static
string
ShowUrls(
string
text)
{
//
代码来自博客园
http://www.cnblogs.com
Regex linkRegex
=
new
Regex(
"
href\\s*=\\s*(?:(?:\\\
"
(
?<
url
>
[
^
\\\
"
]*)\\\
"
)
|
(
?<
url
>
[
^
\\s]
*
))
"
,
RegexOptions.IgnoreCase
|
RegexOptions.Compiled);
MatchCollection linkMatchs
=
linkRegex.Matches(text);
string
pattern
=
@"
(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])
"
;
MatchCollection matchs;
string
clearText
=
Regex.Replace(text,
"
(<br>|<br/>)
"
,
"
"
, RegexOptions.IgnoreCase);
clearText
=
Regex.Replace(clearText,
"
<[^>]*>
"
,
string
.Empty, RegexOptions.Compiled);
//
清除html标记
matchs
=
Regex.Matches(clearText, pattern, RegexOptions.IgnoreCase
|
RegexOptions.Compiled);
bool
flag1
=
true
;
for
(
int
i
=
0
;i
<
matchs.Count;i
++
)
{
Match m
=
matchs[i];
string
link
=
"
<a href=\
""
+ m.ToString() +
"
\
"
target=\
"
_blank\
"
>
"
+
m.ToString()
+
"
</a>
"
;
if
(linkMatchs.Count
>
0
)
{
foreach
(Match linkMatch
in
linkMatchs)
{
if
(linkMatch.Value.IndexOf(m.Value)
>
-
1
)
{
flag1
=
false
;
break
;
}
}
}
if
(flag1)
{
bool
flag2
=
true
;
for
(
int
j
=
0
; j
<
i; j
++
)
{
if
(m.ToString()
==
matchs[j].ToString())
{
flag2
=
false
;
}
}
if
(flag2)
{
text
=
text.Replace(m.ToString(), link);
}
}
}
return
text;
}
查看全文
相关阅读:
DataGrid数据格式设置表达式
删除确认按钮
获取CpuID
R0~R31寄存器
动态改变asp.net网页的标题
使用"Infragistics"问题集
Read Cpu Id
操作DataRow记录
用Javascript创建"后退"按钮
日历控件的“星期几”变为“几”
原文地址:https://www.cnblogs.com/dudu/p/1006119.html
最新文章
Delphi 2007 先睹为快
用VS.NET 2003开发和调用Web Service实例(转帖)
asp.net中如何打印ReportViewer报表
.Net中的反射使用入门(转帖)
万恶的邮箱[转]
Xml WebService完全实例解析(转)
win2003声音
VS 2008 和 .NET 3.5 Beta 2 发布了
android数据库储方式(一)SQLite的基本操作
用MVC搭建班级站点注册界面:数据注解的使用
热门文章
面试常备题jsp基础知识
MVC入门小示例淘宝商品的搜索和价格筛选
菜鸟谈设计模式观察者模式
MVC数据库学习(一)使用实体框架自动更新数据库
android手机上实现歌词同步
android手机上实现竖直seekbar的EQ均衡器
面试常备题插入排序
面试常备题JVM加载class文件的原理机制
水晶报表10高级开发版下载及序列号
20句值得一听的话
Copyright © 2011-2022 走看看