我怎么可能完成使用LINQ和字符串解析这个例子吗? [英] How might I complete this example using LINQ and string parsing?

查看:124
本文介绍了我怎么可能完成使用LINQ和字符串解析这个例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个简单的程序,将在单独的文件夹中的文件进行比较。我目前使用LINQ到对象来解析该文件夹,并希望包括来自于我的结果集以及字符串提取的信息。

I'm trying to write a simple program that will compare the files in separate folders. I'm currently using LINQ to Objects to parse the folder and would like to included information extracted from the string in my result set as well.

下面是我到目前为止所

FileInfo[] fileList = new DirectoryInfo(@"G:\Norton Backups").GetFiles();

var results = from file in fileList
              orderby file.CreationTime
              select new { file.Name, file.CreationTime, file.Length };

foreach (var x in results)
    Console.WriteLine(x.Name);

这会产生:

AWS025.sv2i
AWS025_C_Drive038.v2i
AWS025_C_Drive038_i001.iv2i
AWS025_C_Drive038_i002.iv2i
AWS025_C_Drive038_i003.iv2i
AWS025_C_Drive038_i004.iv2i
AWS025_C_Drive038_i005.iv2i    
...

我想修改LINQ查询,以便:

I would like to modify the LINQ query so that:


  • 这不仅包括实际的备份的文件(你可以告诉因为 _C_Drive038 在上面的例子中,虽然 038 和可能的驱动器盘符可能会改变)。

  • 我想包括如果一个字段文件是主的备份文件(即,它不具有 _i0XX 在文件名的末尾)。

  • 我想包括文件的图像编号(例如,在这种情况下,它是 038 )。

  • 我想包括增加数量,如果它是一个基本文件的incrememnt(如 001 将是一个增量数字)

  • It only includes actual "backup" files (you can tell the backup files because of the _C_Drive038 in the examples above, though 038 and possibly the drive letter could change).
  • I want to include a field if the file is the "main" backup file (i.e., it doesn't have _i0XX at the end of the file name).
  • I want to include the "image number" of the file (e.g. in this case it's 038).
  • I want to include the increment number if it's an incrememnt of a base file (e.g. 001 would be an increment number)

我相信查询的基本布局会是什么样子下面,但我不知道如何最好地完成它(我已经得到了一些如何这可能是做了一些想法,但我有兴趣听到别人可能会怎么做):

I believe the basic layout of the query would look like the following, but I'm not sure how best to complete it (I've got some ideas for how some of this might be done, but I'm interested to heard how others might do it):

var results = from file in fileList
              let IsMainBackup = \\ ??
              let ImageNumber = \\ ??
              let IncrementNumber = \\ ??
              where \\ it is a backup file.
              orderby file.CreationTime
              select new { file.Name, file.CreationTime, file.Length, 
                           IsMainBackup, ImageNumber, IncrementNumber };

在寻找图像NUMBER IncrementNumber ,我想假设这个数据的位置并不总是固定的,意思,我想知道的解析这个一个很好的方式(如果需要的正则表达式, 。请解释如何我可能会使用它)

When looking for the ImageNumber and IncrementNumber, I would like to assume that the location of this data is not always fixed, meaning, I'd like to know of a good way to parse this (If this requires RegEx, please explain how I might use it).

注:我的大多数在解析文本以往的经验使用基于位置的字符串函数,如涉及 MID 。如果有更好的方法我宁愿不依傍那些

NOTE: Most of my past experience in parsing text involved using location-based string functions, such as LEFT, RIGHT, or MID. I'd rather not fall back on those if there is a better way.

推荐答案

使用正则表达式:

    Regex regex = new Regex(@"^.*(?<Backup>_\w_Drive(?<ImageNumber>\d+)(?<Increment>_i(?<IncrementNumber>\d+))?)\.[^.]+$");
    var results = from file in fileList
                  let match = regex.Match(file.Name)
                  let IsMainBackup = !match.Groups["Increment"].Success
                  let ImageNumber = match.Groups["ImageNumber"].Value
                  let IncrementNumber = match.Groups["IncrementNumber"].Value
                  where match.Groups["Backup"].Success
                  orderby file.CreationTime
                  select new { file.Name, file.CreationTime, file.Length,
                               IsMainBackup, ImageNumber, IncrementNumber };

下面是正则表达式的描述:

Here is a description of the regular expression:

^                   Start of string.
.*                  Allow anything at the start.
(?<Backup>...)      Match a backup description (explained below).
\.                  Match a literal period.
[^.]+$              Match the extension (anything except periods).
$                   End of string.



备份是:

Backup is:

_\w_Drive           A literal underscore, any letter, another underscore, then the string "Drive".
(?<ImageNumber>\d+) At least one digit, saved as ImageNumber.
(?<Increment>...)?  An optional increment description.



增量为:

Increment is:

_i                      A literal underscore, then the letter i.
(?<IncrementNumber>\d+) At least one digit, saved as IncrementNumber.






下面是我使用的测试代码:


Here is the test code I used:

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        FileInfo[] fileList = new FileInfo[] {
            new FileInfo("AWS025.sv2i"),
            new FileInfo("AWS025_C_Drive038.v2i"),
            new FileInfo("AWS025_C_Drive038_i001.iv2i"),
            new FileInfo("AWS025_C_Drive038_i002.iv2i"),
            new FileInfo("AWS025_C_Drive038_i003.iv2i"),
            new FileInfo("AWS025_C_Drive038_i004.iv2i"),
            new FileInfo("AWS025_C_Drive038_i005.iv2i")
        };

        Regex regex = new Regex(@"^.*(?<Backup>_\w_Drive(?<ImageNumber>\d+)(?<Increment>_i(?<IncrementNumber>\d+))?)\.[^.]+$");
        var results = from file in fileList
                      let match = regex.Match(file.Name)
                      let IsMainBackup = !match.Groups["Increment"].Success
                      let ImageNumber = match.Groups["ImageNumber"].Value
                      let IncrementNumber = match.Groups["IncrementNumber"].Value
                      where match.Groups["Backup"].Success
                      orderby file.CreationTime
                      select new { file.Name, file.CreationTime,
                                   IsMainBackup, ImageNumber, IncrementNumber };

        foreach (var x in results)
        {
            Console.WriteLine("Name: {0}, Main: {1}, Image: {2}, Increment: {3}",
                x.Name, x.IsMainBackup, x.ImageNumber, x.IncrementNumber);
        }
    }
}



这里是输出I得到:

And here is the output I get:

Name: AWS025_C_Drive038.v2i, Main: True, Image: 038, Increment:
Name: AWS025_C_Drive038_i001.iv2i, Main: False, Image: 038, Increment: 001
Name: AWS025_C_Drive038_i002.iv2i, Main: False, Image: 038, Increment: 002
Name: AWS025_C_Drive038_i003.iv2i, Main: False, Image: 038, Increment: 003
Name: AWS025_C_Drive038_i004.iv2i, Main: False, Image: 038, Increment: 004
Name: AWS025_C_Drive038_i005.iv2i, Main: False, Image: 038, Increment: 005

这篇关于我怎么可能完成使用LINQ和字符串解析这个例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆