2

我正在开发一个从页面上的文本文件中获取文本的应用程序。 示例链接: http ://test.com/textfile.txt

此文本文件包含以下文本:

1 Milk Stuff1.rar
2 Milk Stuff2.rar
3 Milk Stuff2-1.rar
4 Union Stuff3.rar

我正在尝试做的事情如下,从每一行中删除所有内容,除了以'Stuff'开头并以'.rar'结尾的“单词”。

问题是,大多数简单的解决方案,如使用 .Remove、.Split 或 .Replace 最终都会失败。这是因为,例如,使用空格格式化字符串最终会返回:

1
Milk
Stuff1.rar\n2
Milk
Stuff2.rar\n3
Milk
Stuff2-1.rar\n4
Union
Stuff3.rar\n

我敢打赌这并不像看起来那么难,但我会感谢你能给我的任何帮助。

Ps:为了清楚起见,这就是我希望它返回的内容:

Stuff1.rar
Stuff2.rar
Stuff2-1.rar
Stuff3.rar

我目前正在使用此代码:

            client.HeadOnly = true;
            string uri = "http://test.com/textfile.txt"; 

            byte[] body = client.DownloadData(uri);
            string type = client.ResponseHeaders["content-type"]; 
            client.HeadOnly = false; 

            if (type.StartsWith(@"text/")) 
            {
                string[] text = client.DownloadString(uri);

                foreach (string word in text)
                {
                    if (word.StartsWith("Patch") && word.EndsWith(".rar"))
                    {
                        listBox1.Items.Add(word.ToString());
                    }
                }
            }

这显然是行不通的,但你明白了。

先感谢您!

4

3 回答 3

5

这应该有效:

        using (var writer = File.CreateText("output.txt"))
        {
            foreach (string line in File.ReadAllLines("input.txt"))
            {
                var match = Regex.Match(line, "Stuff.*?\\.rar");

                if (match.Success)
                    writer.WriteLine(match.Value);
            }
        }
于 2010-10-20T08:52:45.687 回答
2

我很想对这类事情使用正则表达式。

就像是

Stuff[^\s]*.rar

将只提取您需要的文本。

像这样的功能怎么样:

public static IEnumerable<string> GetStuff(string fileName)
{
    var regex = new Regex(@"Stuff[^\s]*.rar");
    using (var reader = new StreamReader(fileName))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            var match = regex.Match(line);
            if (match.Success)
            {
                yield return match.Value;
            }
        }
    }
}
于 2010-10-20T08:52:38.420 回答
0
for(string line in text)
{
    if(line.EndsWith(".rar"))
    {
        int index = line.LastIndexOf("Stuff");
        if(index != -1)
        {
            listBox1.Items.Add(line.Substring(index));
        }
    }
}
于 2010-10-20T08:53:44.733 回答