【问题标题】:Parsing text in a pattern [closed]以模式解析文本[关闭]
【发布时间】:2014-01-24 23:27:44
【问题描述】:

我正在尝试在 c# 中解析此文本:http://pastebin.com/4Y7ggJjr,但我真的不知道该怎么做。

好像有4个空行,然后是信息等等。

有人知道我应该如何处理这个问题吗?

【问题讨论】:

  • 到目前为止您尝试过什么?正则表达式似乎很适合这类问题。
  • @wdosanjos 我用流式阅读器尝试了一些东西。真的没搞定。但我会尝试使用一些使用 ragex 来解析它的方法。我会回来的
  • 这个问题似乎是题外话,因为它缺乏任何努力/研究
  • @Tokfrans 你能解释为什么它没有成功吗?在使用 ReadLine 方法之前,我尝试过类似的操作。我也许能帮上忙。基本上我会做一个while循环,跳过空白行,直到它完成搜索整个文本。
  • @chris_techno25 这正是我所做的。但我无法解析同一行的数据。例如在第 6 行。我想要两件事,数字 (1) 和名称 (LRAD)

标签: c# parsing text


【解决方案1】:

您可以使用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]

【讨论】:

  • 谢谢。做得很好。
【解决方案2】:
string[] lines = stringWithAllTextFromTheFileInIt.Split(new string[] { "\r\n", StringSplitOptions.RemoveEmptyEntries });

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

【讨论】:

    【解决方案3】:

    你试过了吗:

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

    然后,您可以遍历您的值,因为它们被收集在一个数组中。

    【讨论】:

      【解决方案4】:
      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 个项目。

      【讨论】:

      • @TimSchmelter 不知道这些方法受版权保护。 SO充满了类似的答案。
      • @TimSchmelter 迟到 17 分钟并不意味着它无效。我不想扔掉它并张贴了它。顺便说一句:有很多答案选择索引然后使用该索引进行分组如果您认为这个答案相同,为什么要发布该答案而不是引用其中一个。
      • @TimSchmelter 所以既然你认为你的答案更好我应该删除我的,因为我迟到了 17 分钟并使用了ReadLines 和 IsNullOrWhiteSpace ?
      猜你喜欢
      • 1970-01-01
      • 2013-10-12
      • 2014-09-10
      • 2010-12-23
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多