如这个在一个TEXTBOX里面输入一个远程文本地址,远程地址如下:(既然是请教,请放心!!)
http://www.xnbird.cn/text.txt
我想自动提取的是远程文本其中的这个部分
[Server]
到
[UpLogin]
的中间部分 提取到的这部分我想把它的格式自动转换为: Server1=幻灵游侠
Ip1=58.221.28.30:9958
Pic1=Server1
ServerName1=幻灵游侠一区 Server2=幻灵游侠
Ip2=58.221.28.30:9958
Pic2=Server1
ServerName2=幻灵游侠50区 其中PIC是固定pic*=Server1加上去的 注:*是跟随个数从上往下依次递增 然后上面的格式如:(Server1,Ip1,Pic1,ServerName1)有多少个IP就依次递增一直排下来,每组IP留一个间隔,如上所示。 有重复的IP只提取一次!!!,其他的过滤掉
如果哪位仁兄理解我的意思,能不能帮小弟一下,花点时间帮我设计一下代码呢,小弟菜鸟哈,代码懂得不多,感谢好心的朋友们!
http://www.xnbird.cn/text.txt
我想自动提取的是远程文本其中的这个部分
[Server]
到
[UpLogin]
的中间部分 提取到的这部分我想把它的格式自动转换为: Server1=幻灵游侠
Ip1=58.221.28.30:9958
Pic1=Server1
ServerName1=幻灵游侠一区 Server2=幻灵游侠
Ip2=58.221.28.30:9958
Pic2=Server1
ServerName2=幻灵游侠50区 其中PIC是固定pic*=Server1加上去的 注:*是跟随个数从上往下依次递增 然后上面的格式如:(Server1,Ip1,Pic1,ServerName1)有多少个IP就依次递增一直排下来,每组IP留一个间隔,如上所示。 有重复的IP只提取一次!!!,其他的过滤掉
如果哪位仁兄理解我的意思,能不能帮小弟一下,花点时间帮我设计一下代码呢,小弟菜鸟哈,代码懂得不多,感谢好心的朋友们!
WebClient(http://msdn.microsoft.com/zh-tw/library/system.net.webclient(VS.80).aspx)
或者
HttpWebRequest(http://msdn.microsoft.com/zh-tw/library/system.net.httpwebresponse(VS.80).aspx)
获得这个文本接下来就是分析字符串了
什么indexof substring split等等没啥难度了
你可以把IP放到一个List<string>里面
然后每次读取的时候都先判断下是否存在
private void GetWebText()
{
string mURL = "http://www.xnbird.cn/text.txt";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(mURL);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
string mWebText = reader.ReadToEnd();
response.Close();
string str="";
string strResult = ""; Regex re = new Regex(@"(?<=\[Server\]\s+)[\s\S]+(?=\s+\[UpLogin\])");
mWebText = re.Match(mWebText).Value;
re = new Regex(@"[^\=]*?\=\|(?<ServerName>[^\|]+?)\|(?<Server>[^\|]+?)\|(?<IP>[^\|]+?)\|(?<Port>\d+)\|.+?\n");
MatchCollection mMatches = re.Matches(mWebText);
List<string> iplist = new List<string>();
int index = 1;
StreamWriter sw = File.CreateText(@"c:\a.txt");
foreach (Match m in mMatches)
{
if (!iplist.Contains(m.Groups["IP"].Value))
{
iplist.Add(m.Groups["IP"].Value);
sw.WriteLine("Server" + index.ToString() + "=" + m.Groups["Server"].Value);
sw.WriteLine("Ip" + index.ToString() +"="+ m.Groups["IP"].Value + ":" + m.Groups["Port"].Value);
sw.WriteLine("Pic" + index.ToString() + "=Server1");
sw.WriteLine("ServerName" + index.ToString() + "=" + m.Groups["ServerName"].Value);
sw.WriteLine("");
index++;
strResult += str;
} }
sw.Close();
}