日期:2014-05-17  浏览次数:20391 次

这种正则表达式怎么写?
例如:<a href="/test.jsp?id=2" title="新闻标题" class="a01">新闻标题aaa</a>

我想把链接地址、链接名称提取出来,正则表达式分别怎么写?

即提取/test.jsp?id=2和新闻标题aaa分别怎么写?

------解决方案--------------------
(?i)<a[^>]*?href=(['""]?)(?<href>[^'""]*?)\1[^>]*?>(?<text>[^<>]*?)</a>

.Groups["href"].Value

.Groups["text"].Value

------解决方案--------------------
(?i)<a[^>]*?href=(["']?)(?<href>[^"']*?)\1[^>]*?>(?<content>.*?)</a>

.Groups["href"].Value

.Groups["content"].Value
------解决方案--------------------
加入class
测试
string input = @"<a href=""/test.jsp?id=2"" title=""新闻标题"" class=""a01"">新闻标题aaa</a>";
            string pattern = @"(?i)<a[^>]*?href=(['""]?)(?<href>[^'""]*?)\1[^>]*?class=(['""]?)a01\2[^>]*?>(?<text>[^<>]*?)</a>";
            Match m = Regex.Match(input,pattern);
            string v1 = m.Groups["href"].Value;///test.jsp?id=2
            string v2 = m.Groups["text"].Value;//新闻标题aaa

------解决方案--------------------
正则
------解决方案--------------------
這哪有什麼正則
------解决方案--------------------
引用:
加入class
测试
string input = @"<a href=""/test.jsp?id=2"" title=""新闻标题"" class=""a01"">新闻标题aaa</a>";
            string pattern = @"(?i)<a[^>]*?href=(['""]?)(?<href>[^'""]*?)\1[^>]*?class=(['""]?)a01\2[^>]*?>(?<text>[^<>]*?)</a>";
            Match m = Regex.Match(input,pattern);