.net 用于查找超链接的查询字符串部分的正则表达式

.net 用于查找超链接的查询字符串部分的正则表达式,.net,regex,.net,Regex,我对正则表达式很陌生,但我相信正则表达式会给我带来比字符串方法更优雅的东西 我有一个超链接,形式如下: <a href="http://server.com/default.aspx?abc=123">hello</a> 我只想拔出询问部分。另外,什么是.net正则表达式的好参考(害羞的咧嘴笑)?我觉得很难理解。对于regex开发,我推荐。至于正则表达式本身,搜索?和匹配到下一个“对于正则表达式开发,我建议。对于正则表达式本身,搜索?并匹配到下一个” 编辑:警惕贪婪

我对正则表达式很陌生,但我相信正则表达式会给我带来比字符串方法更优雅的东西

我有一个超链接,形式如下:

<a href="http://server.com/default.aspx?abc=123">hello</a>


我只想拔出询问部分。另外,什么是.net正则表达式的好参考(害羞的咧嘴笑)?我觉得很难理解。

对于regex开发,我推荐。至于正则表达式本身,搜索?和匹配到下一个“

对于正则表达式开发,我建议。对于正则表达式本身,搜索?并匹配到下一个”

编辑:警惕贪婪。。对于懒惰(如果有其他属性),请使用此选项

/\?(.+?)"/
谢谢@Guffa

编辑:警惕贪婪。。对于懒惰(如果有其他属性),请使用此选项

/\?(.+?)"/

谢谢@Guffa

也许简单点

/\?([^\"]+)\"/

它会匹配abc=123

也许简单点

/\?([^\"]+)\"/

它将匹配
abc=123

以下代码将提取查询字符串

string html = "<a href=\"http://server.com/default.aspx?abc=123\">hello</a>";
Match m = Regex.Match(html, "<a[^>]+href=\".*?\\?(.*?)\">");
string querystring = m.Groups[1].ToString();
string html=”“;
Match m=Regex.Match(html,“]+href=\”*?\?(.*?\”>”);
字符串querystring=m.Groups[1].ToString();
regex解释说:

take only strings starting with <a href="
between the a and href there can be other attributes, spaces, it ignores them
make a group of the the url, from the first question mark to the ending quotes - this is your query string

仅获取以开头的字符串以下代码将提取查询字符串

string html = "<a href=\"http://server.com/default.aspx?abc=123\">hello</a>";
Match m = Regex.Match(html, "<a[^>]+href=\".*?\\?(.*?)\">");
string querystring = m.Groups[1].ToString();
string html=”“;
Match m=Regex.Match(html,“]+href=\”*?\?(.*?\”>”);
字符串querystring=m.Groups[1].ToString();
regex解释说:

take only strings starting with <a href="
between the a and href there can be other attributes, spaces, it ignores them
make a group of the the url, from the first question mark to the ending quotes - this is your query string
只取以开头的字符串,这个正则表达式应该有效

(?<=href="[^"]+\?)[^"]+
(?这个正则表达式应该可以工作

(?<=href="[^"]+\?)[^"]+

(?而不是正则表达式,您不能只使用类,特别是属性吗

例如:

Uri uri = new Uri("http://server.com/default.aspx?abc=123");
Console.WriteLine(uri.Query);
印刷品:

?abc=123


除了正则表达式,您不能只使用类,特别是属性吗

例如:

Uri uri = new Uri("http://server.com/default.aspx?abc=123");
Console.WriteLine(uri.Query);
印刷品:

?abc=123


字符类[]后不需要a+?字符类[]后不需要a+?将其设置为/\?(.+?)“/,以便它在第一个qoation标记处停止,以防href之后有更多属性。我需要将其进一步修改为/\?([^\s]*?)“/-因为?(空查询字符串)仍然是有效的URL/href=“*\?([^\s]*?)”/--修改以适应?在前面的属性中,如“更好地使用
/
@Gumbo wudn,如果href不是第一个属性,这不会失败吗?将其设置为/\?(.+?)”/以便它在第一个qoation标记处停止,以防在href之后有更多属性。我将它进一步修改为/\?([^\s]*?)/-因为?(空查询字符串)仍然是有效的URL/href=“.\?([^\s]*?)”/--修改以适应?在以前的属性中,如“更好地使用
/
@Gumbo wudn”中,如果href不是第一个属性,这不是失败吗?好建议。这应该也是可行的。但听起来他仍然需要解析HTML文件以获得链接。好建议。这也应该是可行的。但听起来他似乎是可行的仍然需要解析HTML文件以获取链接。