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;
}
查看全文
相关阅读:
C++ 面向对象编程3 封装 继承 多态
C++ 面向对象编程2
C++ 面向对象编程1
C++开发环境和基础语法
RTOS概述
STM32F4 窗口看门狗(WWDG)
STM32F407 独立看门狗 (IWDG)
DHT11温湿度传感器
Ubuntu20.04安装、配置、卸载QT5.9.9与QT creator以及第一个编写QT程序
Linux,Ubuntu20.04LTS环境下安装JDK1.8和IDEA2021
原文地址:https://www.cnblogs.com/dudu/p/1006119.html
最新文章
Beta阶段对团队成员公开感谢
对团队成员公开感谢
作业要求 20181127-1 附加作业 软件工程原则的应用实例分析
每周例行报告(11.28-12.4)
20181120-1 每周例行报告
20181113-2 每周例行报告
20181030-4 每周例行报告
作业要求 20181023-3 每周例行报告
VirtualBox中CentOS 7 NAT+Host only网络配置
CentOS 7修改默认启动界面
热门文章
CentOS 7防火墙操作
CentOS7设置host name
IDE添加文件头@author信息
idea添加自定义live template模板
eclipse、idea、notepad++设置编码为UTF-8,换行符为Unix
eclipse、idea、notepad++将tab键输入设置为四个空格
向IDE导入阿里编码规约格式化模板和注释模板
IDE安装阿里编码规约检查插件
C++ 面向对象编程7 模板
C++ 面向对象编程4 运算符重载
Copyright © 2011-2022 走看看