c# -删除列表1 &清单3中的清单2

本文关键字:清单 删除 删除列 列表 | 更新日期: 2023-09-27 18:02:08

我有一个文件,我正在阅读,分割成不同的列表,并将它们输出到RichTextBox,然后被读取到3个不同的列表框。我目前正在做这一切,但我遇到了一些我不知道如何修复/工作周围。

我的代码如下,我似乎很难理解为什么它不能正常工作,当它到达Match twoRegex = Regex.Match(…)部分的代码。

代码:

    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?
  • 有了这个,有办法解决它吗?
  • 请建议!:)

c# -删除列表1 &清单3中的清单2

您是否错过了在两行之间的第二个正则表达式中的管道字符?

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