C# - 正则表达式问题查找/显示RichTextBoxes的行

时间:2011-07-13 22:14:40

标签: c# regex richtextbox match lines

我要做的是格式化文件并将其分类为3个不同的RichTextBox,具体取决于与.txt文件中的行匹配的正则表达式。

我打开的文本文件如下所示:

POS     INFO        XINFO    YINFO    INFO  whatIWantToMatch  
J6      INT-00113G  227.905  174.994  180   SOIC8     
J3      INT-00113G  227.905  203.244  180   SOIC8     
U13     EXCLUDES    242.210  181.294  180   QFP128    
U3      IC-00276G   236.135  198.644  90    BGA48     
U12     IC-00270G   250.610  201.594  0     SOP8      
J1      INT-00112G  269.665  179.894  180   SOIC16    
J2      INT-00112G  269.665  198.144  180   SOIC16    
C44     EXCLUDES    237.910  193.469  0     0603_5    
C45     EXCLUDES    244.102  193.387  0     0603_5    
C76     CAP-00117G  227.710  198.594  0     0603_5    
C13     EXCLUDES    245.044  191.416  90    0402_2    
R12     RES-00458G  246.560  202.694  90    0402_2  

这是我的代码:

    private void GCFormatButton_Click(object sender, EventArgs e)
    {
        // Resets the text in the placement rich text boxes.
        placementOneRichTextBox.ResetText();
        placementTwoRichTextBox.ResetText();
        userDefinedRichTextBox.ResetText();

        formatHelper();
        listFormatHelper();
    }

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

            // Creates lists for the lines to be stored in.
            var placementOneList = new List<string>();
            var placementTwoList = new List<string>();
            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;

                placementOneList.Add(line);
                placementTwoList.Add(line);
                placementUserDefinedList.Add(line);
            }

            // Handles all of the requirements for placement type one.
            placementOneList = findPackagePlacementOneType(placementOneList);

            // Prints the formatted refs to the richtextbox.
            foreach (var line in placementOneList)
                placementOneRichTextBox.AppendText(line + "\n");

            // Handles the requirements for placement type two.
            placementTwoList = findPackagePlacementTwoType(placementTwoList);

            // Prints the formatted refs to the richtextbox.
            foreach (var line in placementTwoList)
                placementTwoRichTextBox.AppendText(line + "\n");

            // Handles all of the requirements for placement type one.
            placementUserDefinedList = findPackagePlacementChoiceType(placementUserDefinedList);

            // Prints the formatted refs to the richtextbox.
            foreach (var line in placementUserDefinedList)
                userDefinedRichTextBox.AppendText(line + "\n");
        }

        // Catches an exception if the file was not opened.
        catch (Exception)
        {
            MessageBox.Show("Could not format the text.", "Formatting Text Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    private void listFormatHelper()
    {
        // Splits the lines in the rich text boxes
        var listOneLines = placementOneRichTextBox.Text.Split('\n');
        var listTwoLines = placementTwoRichTextBox.Text.Split('\n');
        var listUserLines = userDefinedRichTextBox.Text.Split('\n');

        // Resest the text in the listboxes
        placementOneListBox.ResetText();
        placementTwoListBox.ResetText();
        userDefinedListBox.ResetText();

        // Set the selection mode to multiple and extended.
        placementOneListBox.SelectionMode = SelectionMode.MultiExtended;
        placementTwoListBox.SelectionMode = SelectionMode.MultiExtended;
        userDefinedListBox.SelectionMode = SelectionMode.MultiExtended;

        //placementOneListBox.Data
        // Shutdown the painting of the ListBox as items are added.
        placementOneListBox.BeginUpdate();
        placementTwoListBox.BeginUpdate();
        userDefinedListBox.BeginUpdate();

        // Display the items in the listbox.
        placementOneListBox.DataSource = listOneLines;
        placementTwoListBox.DataSource = listTwoLines;
        userDefinedListBox.DataSource = listUserLines;

        // Allow the ListBox to repaint and display the new items.
        placementOneListBox.EndUpdate();
        placementTwoListBox.EndUpdate();
        userDefinedListBox.EndUpdate();
    }

    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, @"0603.*");
            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;
    }

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

但是,使用我一直使用的REGEX,我不能正确匹配我想要的。我想在文本文件“whatIWantToMatch”中匹配上面标记的文件的结尾。此外,由于某种原因,函数“findPackagePlacementChoiceType”获得了一些与“findPackagePlacementOneType”相同的结果,它不应该是。


问题

  • 关于如何制作更好的正则表达式的任何建议?
  • 为什么finePackagePlacementChoiceType与findPackagePlaceMentOneType匹配的结果相似?
  • 为什么findPackagePlacementOneType(和其他函数)没有正确地抓住他们的匹配?
    • 我的意思是,它可以抓住3个“603_5”结尾中的2个而不是全部3个......?

3 个答案:

答案 0 :(得分:1)

这一行看起来不对:

Match regex0402 = Regex.Match(line, @"0603.*");

不应该是:

Match regex0402 = Regex.Match(line, @"0402.*");

答案 1 :(得分:1)

因为您只是OR匹配您的结果,所以将返回任何与这些正则表达式匹配的行。您需要编写一个可能与行结构本身匹配的正则表达式。

例如:

J3 INT-00113G 227.905 203.244 180 SOIC8

可以与

之类的东西匹配
^(\w+\d)\s(\w-\d{5}\w)\s(\d+\.\d+)\s(\d+\.\d+)\s(\d)\s(\w{4}\d)

但是在不知道这些数据如何变化的情况下,我发现很难知道什么可能会在一行之间发生变化。查看正则表达式上的msdn article,并构建一个匹配每个行变化的情况。

修改

好的,仔细检查一下你原来的问题之后,你想要在每一行的末尾匹配一个特定的字符串:

^.+(SOIC8)

匹配以SOIC8结尾的行

  

为什么findPackagePlacementOneType(和其他函数)没有   适当地抓住他们的比赛?

我刚注意到你的一些正则表达式字符串以。*(匹配任何字符,0次或更多次......)使用\.表示句点,如果它始终以数字结尾则具体:\ d

答案 2 :(得分:1)

由于您希望放在同一组中的“whatIWantToMatch”值之间没有多少共同点,并且您还提前知道所有可能的值是什么,您可能需要考虑使用简单的if / else结构而不是正则表达式:

var placementOneList = new List<string>();
var placementTwoList = new List<string>();
var placementUserDefinedList = new List<string>();

// For each line in the file
foreach(string line in File.ReadAllLines("filename"))
{
    // Split the line to get only the "whatIWantToMatch" token
    // (Error handling omitted for simplicity)
    var match = line.Split(new String[] {" ", "\t"}, 
        StringSplitOptions.RemoveEmptyEntries)[5];

    // Put the line in the appropriate list depending upon its "whatIWantToMatch" value
    if(match.StartsWith("RES.") { placementOneList.Add(line); }
    else if(match.StartsWith("0603.") { placementOneList.Add(line); }
    // ...
    else if(match.StartsWith("BGA.") { placementTwoList.Add(line); }
    // ...
    else { throw new ApplicationException(); } // No match found
}