C# - 使用正则表达式将数据排序到列表框/从列表框排序

发布于 2024-11-23 22:09:30 字数 8012 浏览 2 评论 0原文

我有一个包含数据的文件,我想将其分成三个不同的列表框。我希望第一个列表框包含所有数据(从文本文档读取的每一行数据的新列表项)。

主列表框:

D1
D2
C9
R7
R1
C130
D99
Q9

第二个列表框:(使用正则表达式匹配以 D 开头的值之后)

D1
D2
D99

第三个列表框: > (在使用正则表达式匹配以 C 开头的值之后)

C9
C130

并且在这些值与 SECONDTHIRD 列表框匹配之后,

主列表框将如下所示:

R7
R1
Q9  

代码

    static List<string> findPackagePlacementOneType(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // PLACEMENT ONE Regex
            Match regexRES = Regex.Match(line, @"^.+(RES)");
            Match regex0402 = Regex.Match(line, @"^.+(0402)");
            Match regex0201 = Regex.Match(line, @"^.+(0201)");
            Match regex0603 = Regex.Match(line, @"^.+(0603)");
            Match regex0805 = Regex.Match(line, @"^.+(0805)");
            Match regex1206 = Regex.Match(line, @"^.+(1206)");
            Match regex1306 = Regex.Match(line, @"^.+(1306)");
            Match regex1608 = Regex.Match(line, @"^.+(1608)");
            Match regex3216 = Regex.Match(line, @"^.+(3216)");
            Match regex2551 = Regex.Match(line, @"^.+(2551)");
            Match regex1913 = Regex.Match(line, @"^.+(1913)");
            Match regex1313 = Regex.Match(line, @"^.+(1313)");
            Match regex2513 = Regex.Match(line, @"^.+(2513)");
            Match regex5125 = Regex.Match(line, @"^.+(5125)");
            Match regex2525 = Regex.Match(line, @"^.+(2525)");
            Match regex5619 = Regex.Match(line, @"^.+(5619)");
            Match regex3813 = Regex.Match(line, @"^.+(3813)");
            Match regex1508 = Regex.Match(line, @"^.+(1508)");
            Match regex6431 = Regex.Match(line, @"^.+(6431)");
            Match regex2512 = Regex.Match(line, @"^.+(2512)");
            Match regex1505 = Regex.Match(line, @"^.+(1505)");
            Match regex2208 = Regex.Match(line, @"^.+(2208)");
            Match regex1005 = Regex.Match(line, @"^.+(1005)");
            Match regex1010 = Regex.Match(line, @"^.+(1010)");
            Match regex2010 = Regex.Match(line, @"^.+(2010)");
            Match regex0505 = Regex.Match(line, @"^.+(0505)");
            Match regex0705 = Regex.Match(line, @"^.+(0705)");
            Match regex1020 = Regex.Match(line, @"^.+(1020)");
            Match regex1812 = Regex.Match(line, @"^.+(1812)");
            Match regex2225 = Regex.Match(line, @"^.+(2225)");
            Match regex5764 = Regex.Match(line, @"^.+(5764)");
            Match regex4532 = Regex.Match(line, @"^.+(4532)");
            Match regex1210 = Regex.Match(line, @"^.+(1210)");
            Match regex0816 = Regex.Match(line, @"^.+(0816)");
            Match regex0363 = Regex.Match(line, @"^.+(0363)");
            Match regexSOT = Regex.Match(line, @"^.+(SOT)");

            if (regexRES.Success || regex0402.Success || regex0201.Success || regex0603.Success ||
                regex0805.Success || regex1206.Success || regex1306.Success || regex1608.Success ||
                regex3216.Success || regex2551.Success || regex1913.Success || regex1313.Success ||
                regex2513.Success || regex5125.Success || regex2525.Success || regex5619.Success ||
                regex3813.Success || regex1508.Success || regex6431.Success || regex2512.Success ||
                regex1505.Success || regex2208.Success || regex1005.Success || regex1010.Success ||
                regex2010.Success || regex0505.Success || regex0705.Success || regex1020.Success ||
                regex1812.Success || regex2225.Success || regex5764.Success || regex4532.Success ||
                regex1210.Success || regex0816.Success || regex0363.Success || regexSOT.Success)
            {
                result.Add(string.Join(" ", line));
            }

            else
                result.Remove(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    // Finds the placement 2 package types.
    static List<string> findPackagePlacementTwoType(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // PLACEMENT TWO Regex
            Match regexBGA = Regex.Match(line, @"^.+(BGA)");
            Match regexSOP8 = Regex.Match(line, @"^.+(SOP8)");
            Match regexQSOP = Regex.Match(line, @"^.+(QSOP)");
            Match regexTQSOP = Regex.Match(line, @"^.+(TQSOP)");
            Match regexSOIC16 = Regex.Match(line, @"^.+(SOIC16)");
            Match regexSOIC12Plus = Regex.Match(line, @"^.+(SOIC12)");
            Match regexSOIC8 = Regex.Match(line, @"^.+(SOIC8)");
            Match regexSO8 = Regex.Match(line, @"^.+(SO8)");
            Match regexSO08 = Regex.Match(line, @"^.+(SO08)");
            Match regexCQFP = Regex.Match(line, @"^.+(CQFP)");
            Match regexLCC = Regex.Match(line, @"^.+(LCC)");
            Match regexLGA = Regex.Match(line, @"^.+(LGA)");
            Match regexOSCCC = Regex.Match(line, @"^.+(OSCCC)");
            Match regexPLCC = Regex.Match(line, @"^.+(PLCC)");
            Match regexQFN = Regex.Match(line, @"^.+(QFN)");
            Match regexQFP = Regex.Match(line, @"^.+(QFP)");
            Match regexSOJ = Regex.Match(line, @"^.+(SOJ)");
            Match regexSON = Regex.Match(line, @"^.+(SON)");

            if (regexBGA.Success || regexSOP8.Success || regexQSOP.Success || regexTQSOP.Success ||
               regexSOIC16.Success || regexSOIC12Plus.Success || regexSOIC8.Success || regexSO8.Success ||
               regexSO08.Success || regexCQFP.Success || regexLCC.Success || regexLGA.Success ||
               regexOSCCC.Success || regexPLCC.Success || regexQFN.Success || regexQFP.Success ||
               regexSOJ.Success || regexSON.Success)
            {
                result.Add(string.Join(" ", line));
            }

            else
                result.Remove(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    static List<string> findPackagePlacementChoiceType(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // PLACEMENT CHOICE Regex
            Match regexCAP = Regex.Match(line, @"^.+(CAP)");
            Match regexIND = Regex.Match(line, @"^.+(IND)");
            Match regexMELF = Regex.Match(line, @"^.+(MELF)");
            Match regexDIOM = Regex.Match(line, @"^.+(DIOM)");
            Match regexSOD = Regex.Match(line, @"^.+(SOD)");
            Match regexSTO = Regex.Match(line, @"^.+(STO)");
            Match regexTO = Regex.Match(line, @"^.+(TO)");

            if (regexCAP.Success || regexIND.Success || regexMELF.Success || regexDIOM.Success ||
               regexSOD.Success || regexSTO.Success || regexTO.Success)
            {
                result.Add(string.Join(" ", line));
            }

            else
                result.Remove(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

问题

我目前有正则表达式,可以对文件进行排序并将文件分成三个单独的列表框。但是...我宁愿将数据放置在其中一个列表框中,然后进行排序/删除如果它与正确的正则表达式匹配,则从该列表框中获取。

  • 如何将 .txt 文档加载到列表框中(每个新行都是列表框中的一个新项目),然后使用正则表达式遍历主列表框中的每一行以确定这些行是否进入不同的第二个第三个列表框还是留在主列表框中?

I have a file with data in it that I would like to split up into three different ListBoxes. I want the first ListBox to contain all of the data (a new list item for every line of data read from the text document).

Main ListBox:

D1
D2
C9
R7
R1
C130
D99
Q9

Second ListBox: (after a regex was used to match values that start with D)

D1
D2
D99

Third ListBox: (after a regex was used to match values that start with C)

C9
C130

And after those values were matched for the SECOND and THIRD ListBoxes, the

New Main ListBox will look like this:

R7
R1
Q9  

CODE

    static List<string> findPackagePlacementOneType(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // PLACEMENT ONE Regex
            Match regexRES = Regex.Match(line, @"^.+(RES)");
            Match regex0402 = Regex.Match(line, @"^.+(0402)");
            Match regex0201 = Regex.Match(line, @"^.+(0201)");
            Match regex0603 = Regex.Match(line, @"^.+(0603)");
            Match regex0805 = Regex.Match(line, @"^.+(0805)");
            Match regex1206 = Regex.Match(line, @"^.+(1206)");
            Match regex1306 = Regex.Match(line, @"^.+(1306)");
            Match regex1608 = Regex.Match(line, @"^.+(1608)");
            Match regex3216 = Regex.Match(line, @"^.+(3216)");
            Match regex2551 = Regex.Match(line, @"^.+(2551)");
            Match regex1913 = Regex.Match(line, @"^.+(1913)");
            Match regex1313 = Regex.Match(line, @"^.+(1313)");
            Match regex2513 = Regex.Match(line, @"^.+(2513)");
            Match regex5125 = Regex.Match(line, @"^.+(5125)");
            Match regex2525 = Regex.Match(line, @"^.+(2525)");
            Match regex5619 = Regex.Match(line, @"^.+(5619)");
            Match regex3813 = Regex.Match(line, @"^.+(3813)");
            Match regex1508 = Regex.Match(line, @"^.+(1508)");
            Match regex6431 = Regex.Match(line, @"^.+(6431)");
            Match regex2512 = Regex.Match(line, @"^.+(2512)");
            Match regex1505 = Regex.Match(line, @"^.+(1505)");
            Match regex2208 = Regex.Match(line, @"^.+(2208)");
            Match regex1005 = Regex.Match(line, @"^.+(1005)");
            Match regex1010 = Regex.Match(line, @"^.+(1010)");
            Match regex2010 = Regex.Match(line, @"^.+(2010)");
            Match regex0505 = Regex.Match(line, @"^.+(0505)");
            Match regex0705 = Regex.Match(line, @"^.+(0705)");
            Match regex1020 = Regex.Match(line, @"^.+(1020)");
            Match regex1812 = Regex.Match(line, @"^.+(1812)");
            Match regex2225 = Regex.Match(line, @"^.+(2225)");
            Match regex5764 = Regex.Match(line, @"^.+(5764)");
            Match regex4532 = Regex.Match(line, @"^.+(4532)");
            Match regex1210 = Regex.Match(line, @"^.+(1210)");
            Match regex0816 = Regex.Match(line, @"^.+(0816)");
            Match regex0363 = Regex.Match(line, @"^.+(0363)");
            Match regexSOT = Regex.Match(line, @"^.+(SOT)");

            if (regexRES.Success || regex0402.Success || regex0201.Success || regex0603.Success ||
                regex0805.Success || regex1206.Success || regex1306.Success || regex1608.Success ||
                regex3216.Success || regex2551.Success || regex1913.Success || regex1313.Success ||
                regex2513.Success || regex5125.Success || regex2525.Success || regex5619.Success ||
                regex3813.Success || regex1508.Success || regex6431.Success || regex2512.Success ||
                regex1505.Success || regex2208.Success || regex1005.Success || regex1010.Success ||
                regex2010.Success || regex0505.Success || regex0705.Success || regex1020.Success ||
                regex1812.Success || regex2225.Success || regex5764.Success || regex4532.Success ||
                regex1210.Success || regex0816.Success || regex0363.Success || regexSOT.Success)
            {
                result.Add(string.Join(" ", line));
            }

            else
                result.Remove(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    // Finds the placement 2 package types.
    static List<string> findPackagePlacementTwoType(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // PLACEMENT TWO Regex
            Match regexBGA = Regex.Match(line, @"^.+(BGA)");
            Match regexSOP8 = Regex.Match(line, @"^.+(SOP8)");
            Match regexQSOP = Regex.Match(line, @"^.+(QSOP)");
            Match regexTQSOP = Regex.Match(line, @"^.+(TQSOP)");
            Match regexSOIC16 = Regex.Match(line, @"^.+(SOIC16)");
            Match regexSOIC12Plus = Regex.Match(line, @"^.+(SOIC12)");
            Match regexSOIC8 = Regex.Match(line, @"^.+(SOIC8)");
            Match regexSO8 = Regex.Match(line, @"^.+(SO8)");
            Match regexSO08 = Regex.Match(line, @"^.+(SO08)");
            Match regexCQFP = Regex.Match(line, @"^.+(CQFP)");
            Match regexLCC = Regex.Match(line, @"^.+(LCC)");
            Match regexLGA = Regex.Match(line, @"^.+(LGA)");
            Match regexOSCCC = Regex.Match(line, @"^.+(OSCCC)");
            Match regexPLCC = Regex.Match(line, @"^.+(PLCC)");
            Match regexQFN = Regex.Match(line, @"^.+(QFN)");
            Match regexQFP = Regex.Match(line, @"^.+(QFP)");
            Match regexSOJ = Regex.Match(line, @"^.+(SOJ)");
            Match regexSON = Regex.Match(line, @"^.+(SON)");

            if (regexBGA.Success || regexSOP8.Success || regexQSOP.Success || regexTQSOP.Success ||
               regexSOIC16.Success || regexSOIC12Plus.Success || regexSOIC8.Success || regexSO8.Success ||
               regexSO08.Success || regexCQFP.Success || regexLCC.Success || regexLGA.Success ||
               regexOSCCC.Success || regexPLCC.Success || regexQFN.Success || regexQFP.Success ||
               regexSOJ.Success || regexSON.Success)
            {
                result.Add(string.Join(" ", line));
            }

            else
                result.Remove(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    static List<string> findPackagePlacementChoiceType(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // PLACEMENT CHOICE Regex
            Match regexCAP = Regex.Match(line, @"^.+(CAP)");
            Match regexIND = Regex.Match(line, @"^.+(IND)");
            Match regexMELF = Regex.Match(line, @"^.+(MELF)");
            Match regexDIOM = Regex.Match(line, @"^.+(DIOM)");
            Match regexSOD = Regex.Match(line, @"^.+(SOD)");
            Match regexSTO = Regex.Match(line, @"^.+(STO)");
            Match regexTO = Regex.Match(line, @"^.+(TO)");

            if (regexCAP.Success || regexIND.Success || regexMELF.Success || regexDIOM.Success ||
               regexSOD.Success || regexSTO.Success || regexTO.Success)
            {
                result.Add(string.Join(" ", line));
            }

            else
                result.Remove(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

QUESTIONS

I currently have my regular expressions that sort through and divide the file up into three seperate ListBoxes.. However... I would rather have the data placed in one of the ListBoxes and then sorted/removed from that ListBox if it matches the proper regular expression.

  • How do I go about loading the .txt document into the ListBox (with every new line being a new item in the ListBox) and then going through each line in the main ListBox with regex to determine if the lines go into a different second or third ListBox or stay in the main ListBox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2024-11-30 22:09:30

您可以将它们组合成一个,而不是使用多个正则表达式。

您也不需要先调用 Regex.Match,然后调用 match.Success;只需调用Regex.IsMatch即可。

我不明白您为什么使用 result.Remove(line)。您从未添加过该不匹配的行,那么为什么需要删除它呢?

最后,我认为您不需要使该方法特定于使用 List。您可以通过指定 IEnumerable 使其更加通用。

我重写了第一个来演示原理:

static IEnumerable<string> findPackagePlacementOneType(IEnumerable<string> list)
{
    return list.Where(line => Regex.IsMatch(line,
        @"^.+(RES|0402|0201|0603|0805|1206|1306|1608|3216|2551|1913|1313|2513" +
        @"|5125|2525|5619|3813|1508|6431|2512|1505|2208|1005|1010|2010|0505" +
        @"|0705|1020|1812|2225|5764|4532|1210|0816|0363|SOT)"
    ));
}

如果您确实需要一个新的 List,您只需在调用后添加 .ToList() findPackagePlacementOneType

Instead of multiple regexes you could just combine them into one.

You also don’t need to call Regex.Match followed by match.Success; just call Regex.IsMatch.

I don’t understand why you are using result.Remove(line). You never added that non-matching line, so why do you need to remove it?

Finally, I don’t think you need to make the method specific to using List<string>. You can make it more general by specifying IEnumerable<string>.

I’ve rewritten the first one to demonstrate the principle:

static IEnumerable<string> findPackagePlacementOneType(IEnumerable<string> list)
{
    return list.Where(line => Regex.IsMatch(line,
        @"^.+(RES|0402|0201|0603|0805|1206|1306|1608|3216|2551|1913|1313|2513" +
        @"|5125|2525|5619|3813|1508|6431|2512|1505|2208|1005|1010|2010|0505" +
        @"|0705|1020|1812|2225|5764|4532|1210|0816|0363|SOT)"
    ));
}

If you ever really need a new List<string>, you can just add .ToList() after a call to findPackagePlacementOneType.

琉璃梦幻 2024-11-30 22:09:30

我建议创建一些 ObservableCollection,每个 ListBox 一个。 ObservableCollection 在添加或删除项目时引发事件,这允许 ListBox 等内容在发生更改时自动更新。假设它们的名称为 items123。从 items1 = new ObservableCollection(list); 开始,另一个初始化为空集合。另外,修改您发布的方法,以获取单个字符串并返回一个布尔值,说明它是否匹配任何字符串。

第 1 部分,将字符串集合放入 ListBoxes:

mainListBox.ItemsSource = items1;
listBox2.ItemsSource = items2;
listBox3.ItemsSource = items3;

第 2 部分,遍历列表中的每一项:

foreach (string str in items1.ToArray())
{
    if (isPackagePlacementOneType(str))
    {
        items1.Remove(str);
        items2.Add(str);
    } else if ...
}

.ToArray() 是 LINQ 扩展;它是必需的,因为您无法修改正在迭代的枚举。

第 3 部分:此外,我建议根据“不要重复自己”(DRY) 原则对这些方法进行一些修改:

static readonly string[] match1 = new [] { "RES", ... };
static readonly string[] match2 = new [] { "BGA", ... };
static readonly string[] match3 = new [] { "CAP", ... };
//(then in the methods)
return match1.Any(x=>Regex.Match(line, @"^.+(" + x + ")").Success);

这还有一个好处是不需要评估数十个最终根本不会被读取的正则表达式。

I'd recommend making a few ObservableCollection<string>s, one for each of your ListBoxes. ObservableCollections raise events when items are added or removed, which allows things like ListBoxes to automatically update when they change. Say they're called items1, 2, and 3. Start with items1 = new ObservableCollection<string>(list);, and the other initialized as empty collections. Also, rework the methods you posted to take a single string and return a bool saying whether it matched any.

Part 1, putting your collections of strings into the ListBoxes:

mainListBox.ItemsSource = items1;
listBox2.ItemsSource = items2;
listBox3.ItemsSource = items3;

Part 2, going through each item in the list:

foreach (string str in items1.ToArray())
{
    if (isPackagePlacementOneType(str))
    {
        items1.Remove(str);
        items2.Add(str);
    } else if ...
}

The .ToArray() is the LINQ extension; it's needed because you can't modify an enumerable that you're iterating through.

Part 3: Also, I'd recommend reworking the methods a bit, based on the Do not Repeat Yourself (DRY) principle:

static readonly string[] match1 = new [] { "RES", ... };
static readonly string[] match2 = new [] { "BGA", ... };
static readonly string[] match3 = new [] { "CAP", ... };
//(then in the methods)
return match1.Any(x=>Regex.Match(line, @"^.+(" + x + ")").Success);

This also has the benefit of not needing to evaluate dozens of regex's that don't end up being read at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文