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

主列表框:

D1
D2
C9
R7
R1
C130
D99
Q9


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

D1
D2
D99


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

C9
C130


在将这些值与SECOND和THIRD ListBoxes匹配之后,

新的主列表框将如下所示:

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;
    }




问题

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


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

最佳答案

代替多个正则表达式,您可以将它们组合为一个。

您也不需要先打Regex.Match,再打match.Success;只需拨打Regex.IsMatch

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

最后,我认为您不需要专门针对使用List<string>的方法。您可以通过指定IEnumerable<string>使其更通用。

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

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<string>,则只需在调用.ToList()之后添加findPackagePlacementOneType

关于c# - C#-使用Regex对列表框中的数据进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6700967/

10-13 06:53