Я пытаюсь отформатировать файл и отсортировать его по 3 различным RichTextBoxes в зависимости от регулярных выражений, соответствующих строке в файле .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 (и другие функции)) не правильно схватить их матч?
- Я имею в виду, что он может захватить 2 из 3 "603_5" окончаний вместо всех 3 ...?