解析模式中的文本

时间:2014-01-24 23:27:45

标签: c# parsing text

我正在尝试解析此文本:{#3}}在c#中,但我真的不知道应该怎么做。

看起来有4个空行,然后有信息等等。

任何人都知道我应该如何做到这一点?

4 个答案:

答案 0 :(得分:2)

您可以使用File.ReadLines来读取文件中的行,Enumerable.Where来只读取包含文本的行。然后你可以使用GroupBy创建三行组,因为一首歌似乎总是包含三行(持续时间,标题,翻译):

List<Song> songs = File.ReadLines(path)
    .Where(line => !String.IsNullOrWhiteSpace(line))
    .Select((line, index) => new { line, index })  
    .GroupBy(x => x.index / 3)  // integer division trick
    .Select(grp => new Song
    {
        Duration = TimeSpan.Parse(grp.First().line.Trim()),
        Title = grp.ElementAt(1).line.Trim(),
        Interpreter = grp.ElementAt(2).line.Trim()
    }).ToList();

这是一个存储所有信息的自定义类:

public class Song
{
    public TimeSpan Duration { get; set; }
    public string Title {get;set;}
    public string Interpreter { get; set; }
}

修改Here's一个示例,其中包含您输出的示例数据:

1. LRAD from Knife Party [05:15:00]
2. Hymn from His Majesty Andre [05:49:00]
3. Saturn from Kill The Noise, Brillz, Minxx [05:33:00]
4. Wayfarer - Original Mix from Audien [06:00:00]
5. Heads Will Roll - A-Trak Radio Edit from Yeah Yeah Yeahs, A-Trak [03:24:00]
6. Summertime Sadness [Lana Del Rey vs. Cedric Gervais] - Cedric Gervais Remix from Lana Del Rey, Cedric Gervais [06:53:00]
7. EDM Death Machine from Knife Party [04:23:00]
8. Leaving You - Radio Edit from Audien, Michael S. [03:34:00]
9. Astrocat from Pixl [05:22:00]
10. Chronicles Of A Fallen Love - Tom Swoon Remix from The Bloody Beetroots, Greta Svabo Bech [05:41:00]
11. Reason - Radio Edit from Nervo, Hook n Sling [03:34:00]
12. Power Glove from Knife Party [04:22:00]
13. Polar - Original Mix from Fire Flowerz [04:19:00]
14. Enjoy - Oliver Remix from Gigamesh [05:39:00]
15. In My Mind (Axwell Remix) from Ivan Gough & Feenixpawl feat. Georgi Kay [06:41:00]
16. Stellar - Radio Edit from Daddy's Groove [03:19:00]
17. Reason - TV Noise Remix from Liv Nervo, Nervo, Hook n Sling, Miriam Nervo [04:04:00]
18. Galactic Voyage from Pixl [06:36:00]
19. Iron Hill from Fire Flowerz [04:08:00]
20. Harmony from Vicetone [06:17:00]
21. Stars - Original Mix from Vicetone, Jonny Rose [05:15:00]
22. Self Destruct from Pegboard Nerds, Various Artists [03:33:00]
23. Pressure Cooker from Pegboard Nerds, Various Artists [03:14:00]
24. Razor Sharp from Pegboard Nerds & Tristam [04:42:00]
25. Dreams (feat. Laura Brehm) from Rogue, Laura Brehm [04:17:00]
26. Internet Friends - VIP from Knife Party [05:01:00]
27. How We Do - Original Mix from Hardwell, Showtek [05:27:00]
28. Slow Down - Radio Edit from Showtek [03:27:00]
29. Get Loose from Showtek, Noisecontrollers [05:35:00]
30. Unison - Knife Party Remix from Porter Robinson [04:59:00]
31. Stars - Original Mix from Vicetone, Jonny Rose [05:15:00]
32. Flight from Tristam, Braken [03:40:00]
33. Imagine Reality from Uppermost [04:57:00]
34. 5-HT - Kat Krazy Remix from The Good Natured [03:36:00]
35. D.A.N.C.E. from Justice [04:03:00]
36. Alive from Krewella [04:51:00]
37. Lights from Steve Angello, Third Party [05:09:00]
38. We Come Running - Vicetone Remix from Youngblood Hawke [05:48:00]
39. Live for the Night from Krewella [03:28:00]
40. We Come Running - Tiësto Remix from Youngblood Hawke [04:30:00]
41. Damaged - Radio Edit from Adrian Lux [02:46:00]
42. Teenage Crime - Radio Edit from Adrian Lux [02:51:00]
43. You Make Me from Avicii [03:53:00]
44. Wild Child - Radio Edit from Adrian Lux, Marcus Schössow, JJ [02:42:00]

答案 1 :(得分:1)

string[] lines = stringWithAllTextFromTheFileInIt.Split(new string[] { "\r\n", StringSplitOptions.RemoveEmptyEntries });

这将为您提供字符串数组中的所有非空行。如果您根据需要添加更多细节,我可以为您编写正则表达式。

答案 2 :(得分:1)

你试过这个:

    var text = File.ReadAllText("someTextFile.txt");
    var values = text.Trim('\n').Trim('\t').Trim('\r').Split(' '); 

然后,您可以在一个数组中收集值时迭代它们。

答案 3 :(得分:0)

int cnt = 0;
var result = File.ReadLines(filename)
                .Where(line => !String.IsNullOrWhiteSpace(line))
                .GroupBy(line => cnt++ / 3)
                .Select(g => String.Join(Environment.NewLine, g.Select(x => x.Trim())))
                .ToList();

将返回47项。