C# - 删除List1&中的项目时出现问题List3中的List2

时间:2011-07-15 19:37:08

标签: c# regex list text-files

我有一个正在阅读的文件,分成不同的列表并将它们输出到RichTextBox然后被读入3个不同的列表框。我目前正在做所有这些,但是我遇到了一些我不知道如何修复/解决的问题。

我的代码在下面,我似乎无法理解当它到达代码的匹配twoRegex = Regex.Match(...)部分时无法正常工作的原因。 / p>

CODE:

    private void SortDataLines()
    {
        try
        {
            // Reads the lines in the file to format.
            var fileReader = File.OpenText(openGCFile.FileName);

            // Creates a list for the lines to be stored in.
            var placementUserDefinedList = new List<string>();

            // Reads the first line and does nothing with it.
            fileReader.ReadLine();

            // Adds each line in the file to the list.
            while (true)
            {
                var line = fileReader.ReadLine();
                if (line == null)
                    break;

                placementUserDefinedList.Add(line);
            }

            // Creates new lists to hold certain matches for each list.
            var oneResult = new List<string>();
            var twoResult = new List<string>();
            var mainResult = new List<string>();

            foreach (var userLine in placementUserDefinedList)
                mainResult.Add(string.Join(" ", userLine));

            foreach (var oneLine in mainResult)
            {
                // PLACEMENT ONE Regex
                Match oneRegex = Regex.Match(oneLine, @"^.+(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 (oneRegex.Success)
                    oneResult.Add(string.Join(" ", oneLine));
            }

            //
            // THIS IS THE SECTION THAT FAILS..
            // 
            foreach(var twoLine in mainResult)
            {
                //PLACEMENT TWO Regex
                Match twoRegex = Regex.Match(twoLine, @"^.+(BGA|SOP8|QSOP|TQSOP|SOIC16|SOIC12|SOIC8|SO8|SO08"
                    + @"CQFP|LCC|LGA|OSCCC|PLCC|QFN|QFP|SOJ|SON");

                if (twoRegex.Success)
                    twoResult.Add(string.Join(" ", twoLine));
            }

            // Removes the matched values from both of the Regex used above.
            List<string> userResult = mainResult.Except(oneResult).ToList();
            userResult = userResult.Except(twoResult).ToList();


            // Prints the proper values into the assigned RichTextBoxes.
            foreach (var line in userResult)
                userDefinedRichTextBox.AppendText(line + "\n");

            foreach (var line in oneResult)
                placementOneRichTextBox.AppendText(line + "\n");

            foreach (var line in twoResult)
                placementTwoRichTextBox.AppendText(line + "\n");
        }

        // Catches an exception if the file was not opened.
        catch (Exception)
        {
            MessageBox.Show("Could not match any regex values.", "Regular Expression Match Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

问题:

  • 是否有人理解为什么我无法在第二套 REGEX 中找到或失败?
  • 有了这个,有办法解决吗?
  • 建议请! :)

2 个答案:

答案 0 :(得分:5)

你有没有错过两行之间第二个正则表达式中的管道字符?

答案 1 :(得分:2)

Match twoRegex = Regex.Match(twoLine, @"^.+(BGA|SOP8|QSOP|TQSOP|SOIC16|SOIC12|SOIC8|SO8|SO08"
                + @"|CQFP|LCC|LGA|OSCCC|PLCC|QFN|QFP|SOJ|SON)");