У меня есть файл, который я читаю, разбивая на разные списки и выводя их в 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 ?
- С этим, есть ли способ исправить это?
- Предложения, пожалуйста! :)