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;
}
查看全文
相关阅读:
js面向对象:对象创建/继承的演化过程
20189215 2018-2019-2 《密码与安全新技术专题》第3周作业
2018-2019-2 20189215 《网络攻防技术》第二周作业
2018-2019-2 20189215 《网络攻防实践》安全工具研究
20189215 2018-2019-2 《密码与安全新技术专题》第1周作业
2018-2019-2 20189215 《网络攻防技术》第一周作业
2018-2019-1 20189215 《文献管理与信息分析》第三周课程学习总结
2018-2019-1 20189215 《深入理解计算机系统》第三章学习总结
2018-2019-1 20189215 《Linux内核原理与分析》第九周作业
2018-2019-1 20189215 《Linux内核原理与分析》第八周作业
原文地址:https://www.cnblogs.com/dudu/p/1006119.html
最新文章
CentOS/ubuntu/Solaris软件包安装
Python 配置 pip 源
如何优化个人博客的访问速度
BFC布局
[IIS] Windows Server 2016 安裝 URL Rewrite 憑證失效
粒子系统
Java技术之线程通信
Hexo搭建个人博客并部署到Github
20170112meetbug
kvm克隆虚拟机【藏经阁】
热门文章
项目模式(四)—— 全局变量
OKR
MongoDB
vue中如何动态引入图片
css3 :not(选择器) 的简单使用
JS 将数字字符串数组转为 数字数组 (互换),js获取数组对象中 某一个key的值,js判断一个数组是否包含另一个数组(一维数组)
vue项目启动时自动获取ip地址
根据 时区 计算出 指定时间 在 该时区的 时间 ,parseInt的奇特用法
$emit("input")小例子
正则表达式小总结
Copyright © 2011-2022 走看看