正则表达式查找Href值 [英] Regex to find Href value

查看:121
本文介绍了正则表达式查找Href值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中有锚标记,我想知道这些锚标记的href值.我的字符串是这样的:

I have a string in which I have anchor tag I want to know the href values of those anchor tags. my string is like:

This is Test page <a href='test.aspx'>test page</a> .

在此我想找到href的值,即 test.aspx

in this I want to find the value of href i.e. test.aspx

请为此建议我一个好的regx小组.

Please suggest me any good regx group for this.

推荐答案

(如果您使用< a [^>] * href =(?:'(?< href>.*?)')|(?:((?< href>.*?)")),则结果将存储在命名组href

if you use <a [^>]*href=(?:'(?<href>.*?)')|(?:"(?<href>.*?)") then the result will be stored in the named group href

示例:

var inputString="This is Test page <a href='test.aspx'>test page</a>";
var regex=new Regex("<a [^>]*href=(?:'(?<href>.*?)')|(?:\"(?<href>.*?)\")",RegexOptions.IgnoreCase);
var urls=regex.Matches(inputString).OfType<Match>().Select(m =>m.Groups["href"].Value);

urls是包含hrefs的字符串的集合.

urls will be a collection of strings containing the hrefs.

这篇关于正则表达式查找Href值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆