using System;
using System.Text.RegularExpressions;namespace RegexSample {
public class RegExMatcher {
public static void Main(String[] args){
Regex urlRegex = new Regex("(?<url>http://.*)\">(?<singer>.*)-(?<song>.+)</a>");
string[] s = new String[3];
s[0] = "<a href=\"http://music.jztele.com/mp3/yaogun/103.mp3\"> - ado我不能随便说</a>";
s[1] = "<a href=\"http://music.jztele.com/mp3/aniu/我和我的四个妹妹.mp3\"> - 陈庆祥(阿牛)我和我的四个妹妹</a>";
s[2] = "<a href=\"http://av.wanwa.com/fileroot/av/avfiles/19045.rm\">阿妹妹 - Baby让我告诉你</a>"; for (int i=0;i<s.Length;i++)
{
Match m = urlRegex.Match(s[i]); if ( m.Success ) {
if (m.Groups["singer"].Value.ToString().Trim() == "")
{
System.Console.Write("未知歌手");
}
else
{
System.Console.Write(m.Groups["singer"].Value);
}
System.Console.Write("\t");
System.Console.Write(m.Groups["song"].Value);
System.Console.Write("\t");
System.Console.WriteLine(m.Groups["url"].Value);
}
} System.Console.WriteLine();
System.Console.WriteLine("按 Enter 键继续...");
System.Console.ReadLine();
}
}
}
using System.Text.RegularExpressions;namespace RegexSample {
public class RegExMatcher {
public static void Main(String[] args){
Regex urlRegex = new Regex("(?<url>http://.*)\">(?<singer>.*)-(?<song>.+)</a>");
string[] s = new String[3];
s[0] = "<a href=\"http://music.jztele.com/mp3/yaogun/103.mp3\"> - ado我不能随便说</a>";
s[1] = "<a href=\"http://music.jztele.com/mp3/aniu/我和我的四个妹妹.mp3\"> - 陈庆祥(阿牛)我和我的四个妹妹</a>";
s[2] = "<a href=\"http://av.wanwa.com/fileroot/av/avfiles/19045.rm\">阿妹妹 - Baby让我告诉你</a>"; for (int i=0;i<s.Length;i++)
{
Match m = urlRegex.Match(s[i]); if ( m.Success ) {
if (m.Groups["singer"].Value.ToString().Trim() == "")
{
System.Console.Write("未知歌手");
}
else
{
System.Console.Write(m.Groups["singer"].Value);
}
System.Console.Write("\t");
System.Console.Write(m.Groups["song"].Value);
System.Console.Write("\t");
System.Console.WriteLine(m.Groups["url"].Value);
}
} System.Console.WriteLine();
System.Console.WriteLine("按 Enter 键继续...");
System.Console.ReadLine();
}
}
}
string [] singer=new string[10];
string [] name=new string[10];
string [] url=new string[10];
int count=0;
Regex re=new Regex("<a\\s+href=\"(.*)?\">(.*?)-(.*?)<\\/a>");
Match m=re.Match(input);
while(m.Success)
{
if(m.Groups[1].Value.Trim()!="")
{
//url
url[count]=m.Groups[1].Value.Trim();
}
else
{
url[count]="unkown";
}
if(m.Groups[2].Value.Trim()!="")
{
//singer
singer[count]=m.Groups[2].Value.Trim();
}
else
{
url[count]="unkown";
}
if(m.Groups[3].Value.Trim()!="")
{
//name
name[count]=m.Groups[3].Value.Trim();
}
else
{
name[count]="unkown";
}
m.NextMatch();
count++;
}
microsoft.public.cn.dotnet.languages.csharp
还存在一种情况。
<a href="http://music.jztele.com/mp3/yaogun/210.mp3"> - 超载乐队破碎</a>
<a href="ftp://ftp1.mydown.com/home4/sky/down/patch/2k111/f21isp4_1022.zip">《FIFA2001》全新设计的超酷大补帖(附图)下载</a>就是在《FIFA2001》全新设计的超酷大补帖(附图)下载前面没有 - 。。不知道可否解决。这种情况。可以在前面默认家“软件”字样。太感谢了。
还有。在-左右都有两个空格的需要去除掉。谢谢了。
以上各位的分数我一定给。谢谢。但是现在给了要结束帖子的。所以只能明天了。不好意思。
using System.Text.RegularExpressions; string str ="Put Your String Here........"; StringBuilder sb = new StringBuilder();
Regex re = new Regex(@"<a\s+href=([""'])(?<url>[^'"">]+)\1\s*>(\s*(?<name>[^-\s]*)\s*-)?\s*(?<song>[^<]+)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCollection mc = re.Matches(str);
foreach (Match m in mc)
{
if (m.Groups["name"].Value != "")
sb.Append(m.Groups["name"].Value);
else if (m.Groups["name"].Captures.Count > 0)
sb.Append("未知歌手");
else
sb.Append("软件"); sb.Append("\t");
sb.Append(m.Groups["song"].Value);
sb.Append("\t");
sb.Append(m.Groups["url"].Value);
sb.Append("\n");
} str = sb.ToString();