加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式获取HTML标记中的内容(C#)

发布时间:2020-12-14 01:46:18 所属栏目:百科 来源:网络整理
导读:正则表达式获取HTML标记中的内容(C#) // =====================Begin1======================== 试验字符串 string strTmp = .Empty; 正则表达式 tmpStr .Empty; 取出指定HTML标记中的匹配项的值RegexOptions.IgnoreCase忽略大小写,RegexOptions.Multilin

正则表达式获取HTML标记中的内容(C#)

//=====================Begin1========================
试验字符串
stringstrTmp=.Empty;
正则表达式tmpStr.Empty;

取出指定HTML标记中的匹配项的值RegexOptions.IgnoreCase忽略大小写,RegexOptions.Multiline忽略多行显示,
tmpStr="<title>([^<]*)</title>"获取<title>之间内容
strTmp
@"<addkey='ConnectionString'value='server=localhost;database=数据库名;uid=sa;pwd=;pooling=true'/>";
获取“database=”与“;”号之间的字符串:database=(.*);database=([^;]*);;
MatchTitleMatch
Regex.Match(strTmp,tmpStr,RegexOptions.IgnoreCase|RegexOptions.Multiline);

如下例子作语法参考用获取size的值,实际应用可能不会如此复杂><fontcolor='red'size=6>WebForm3</font><;
tmpStr
<(w+s+w+[=]+[']+w+[']+s+[size=]+d)>RegexOptions.Multiline);


取出匹配项的值tmpStrTitleTitleMatch.Groups[1].Value;
替换掉HTML页中所有HTML标记Label1.TextRegex.Replace(Label1.Text.Trim(),<.+?>,0)">"")+*********].Value;

判断匹配正则表达式是否成功if(Regex.Match(tmpStr,0)">).Success)
{
操作}
=====================End1========================

=====================Begin2========================webDocContent<ahref=http://www.xxx.xxx/college/pages/default.htmtarget=_blank>师资队伍</A>解释下面正则表达式:[s]表示匹配空格字符,"+"表示连接strPatterna[s]+href=(?<Link>[^s>]+)[^>]*>(?<Text>[^<]*)</a>获取链接显示的文字MatchCollectionMatchesRegex.Matches(webDocContent,strPattern,0)">RegexOptions.Compiled);
foreach(MatchNextMatchinMatches)
URLNextMatch.Groups[Link].Value.ToString().Trim();
URLTextText].Value.ToString().Trim();
Response.Write(URL
****);
Response.Write(URLText);
}
=====================End2========================

=====================Begin3========================strPageContent.Empty;
StreamReadersrPage
newStreamReader(e:save.txtgb2312));
strPageContent
srPage.ReadToEnd();
srPage.Close();
(/s)*表示0或多个空格符、回车符等,*表示比配0或多个。(.*?)表示除回车符外的所有信息MatchCollectionTitleMatchsRegex.Matches(strPageContent,0)"><tdwidth="85%"class="common_text">((/s)*(.*?)(/s)*(.*?)(/s)*(.*?)(/s)*(.*?)(/s)*)</td>RegexOptions.Multiline);
inttmpNum0循环正则表达式所获取的,满足表达式的内容集合TitleMatchs)
++tmpNum;
Label1.Text
+=<br>****].Value;
}
=====================End3========================

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读