开发者

C# - Problem removing items in List1 & List2 from List3

I have a file that I am reading in, splitting up into different lists and outputting them into RichTextBox to then be read into 3 different Listboxes. I am currently doing all of this, however I have come across something I do not know how to fix/work around.

My code is below and I seem to be having trouble understanding why it fails to properly work when it gets to the Match twoRegex = Regex.Match(...) section of the code.

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开发者_如何学C 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);
        }
    }

QUESTIONS:

  • Does anyone understand why I am unable to to find, or fail at, the second set of REGEX?
  • With that, is there a way to fix it?
  • Suggestions please! :)


Haven't you missed the pipeline character in your second regex between two lines?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜