正则的小问题 string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";获取hello.html和hello2.html 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 var str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";var reg = /href="([^"]+?html)"/g;var urls = [];while(reg.exec(str)){ urls.push(RegExp.$1);}alert(urls) 哦,补充下,我的失误,不好意思那个1=》 1.html 哦 你会C#吗,刚才发错板块了<a href=\"(?<url>.*?)\" target=\"_blank\">.*?</a> 这个是我写的正则 哪儿有问题你的这个在js里OK c#的更简单了不过不懂你的1.html和其他的有什么区别吗 string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>"; MatchCollection mc = Regex.Matches(str,@"(?<=href="")[^\""]+"); foreach (Match m in mc) { Console.WriteLine(m); } 嗯 那好办 有区别,就是说我只获取 target=\"_blank\" 这个链接 string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>"; MatchCollection mc = Regex.Matches(str, @"(?i)(?<=href="")[^\""]+(?=[^>]*?target=""_blank"")"); foreach (Match m in mc) { Console.WriteLine(m); } 稍微严谨点。string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>"; MatchCollection mc = Regex.Matches(str, @"(?i)(?=[^<>]*?target=""_blank"")(?<=href="")[^\""]+"); foreach (Match m in mc) { Console.WriteLine(m); } try... string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>"; Regex reg = new Regex(@"(?i)<a(?=[^>]*?target=""_blank"")[^>]*?href=""([^""]*)"""); MatchCollection mc = reg.Matches(str); foreach (Match m in mc) { richTextBox2.Text += m.Groups[1].Value + "\n"; } 应该是这个吧MatchCollection mc = Regex.Matches(str, @"(?i)(?<=<a\s+href="")[^""]+(?=""\s+target=""_blank"")");不然下面情况会取错的string str = "<a href=\"hello.html\" target=\"_blank\">href=\"coco.html\" target=\"_blank\"</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>"; 谢谢大家的答案,正确的我的这个怎么修改:<a href=\"(?<url>.*?)\" target=\"_blank\">.*?</a> 我想使用这种方式,不用索引的另外关键点解释下就最好了 MatchCollection mc = Regex.Matches(str,@"(?i)(?<=<a\s[^>]*href[^=]*=[^=\x22']*(['\x22]?))((?!\1).)+\.html");foreach(Match m in mc){ m.Value;就是你要的结果。} 嗯 我想 Groups["url"] 这种获取方式,请问怎么修改 在看清楚别人问题的时候再回答,你回答我表示感谢,你骂人我没有必要跟你争论我说的是Groups["url"]这种方式,不是索引的形式,难道你没有看懂? 过客的修改一点。加个名字string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";Regex reg = new Regex(@"(?i)<a(?=[^>]*?target=""_blank"")[^>]*?href=""(?<url>[^""]*)"""); MatchCollection mc = reg.Matches(str);foreach (Match m in mc){ richTextBox2.Text += m.Groups["url"].Value + "\n";} SqlDBHelper类中的方法,如何加上事务机制 C# CS的控件设计 升星 :-) 散分... 日期格式 如何检测WORD文档的格式,急!! 线程中动态加载控件的疑问 C#如何根据数据库数据画简单折线图(winform) 數據的表示形式 dll指针参数C#中如何解决 问问各位,获取摄像头的程序和获取视频采集卡的程序是不是一样的? remoting采用客户激活的方式时,客户端怎样才能不需要服务器对象的dll文件 c# winform ListView.Items.Count=0 问题 线性规划问题,使用对偶单纯形法,如果无解,次优解如何求
var str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
var reg = /href="([^"]+?html)"/g;
var urls = [];
while(reg.exec(str)){
urls.push(RegExp.$1);
}
alert(urls)
那个1=》 1.html
<a href=\"(?<url>.*?)\" target=\"_blank\">.*?</a> 这个是我写的正则 哪儿有问题
你的这个在js里OK
不过不懂你的1.html和其他的有什么区别吗
MatchCollection mc = Regex.Matches(str,@"(?<=href="")[^\""]+");
foreach (Match m in mc)
{
Console.WriteLine(m);
}
有区别,就是说我只获取 target=\"_blank\" 这个链接
MatchCollection mc = Regex.Matches(str, @"(?i)(?<=href="")[^\""]+(?=[^>]*?target=""_blank"")");
foreach (Match m in mc)
{
Console.WriteLine(m);
}
string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
MatchCollection mc = Regex.Matches(str, @"(?i)(?=[^<>]*?target=""_blank"")(?<=href="")[^\""]+");
foreach (Match m in mc)
{
Console.WriteLine(m);
}
Regex reg = new Regex(@"(?i)<a(?=[^>]*?target=""_blank"")[^>]*?href=""([^""]*)""");
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
}
MatchCollection mc = Regex.Matches(str, @"(?i)(?<=<a\s+href="")[^""]+(?=""\s+target=""_blank"")");
不然下面情况会取错的
string str = "<a href=\"hello.html\" target=\"_blank\">href=\"coco.html\" target=\"_blank\"</a><a href=\"1.html\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
我的这个怎么修改:<a href=\"(?<url>.*?)\" target=\"_blank\">.*?</a>
我想使用这种方式,不用索引的
另外关键点解释下就最好了
foreach(Match m in mc)
{
m.Value;就是你要的结果。
}
string str = "<a href=\"hello.html\" target=\"_blank\">hello</a><a href=\"1\">1</a><a href=\"hello2.html\" target=\"_blank\">hello2</a>";
Regex reg = new Regex(@"(?i)<a(?=[^>]*?target=""_blank"")[^>]*?href=""(?<url>[^""]*)""");
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
}