C# - 使用正则表达式将数据排序到列表框/从列表框排序
我有一个包含数据的文件,我想将其分成三个不同的列表框。我希望第一个列表框包含所有数据(从文本文档读取的每一行数据的新列表项)。
主列表框:
D1
D2
C9
R7
R1
C130
D99
Q9
第二个列表框:(使用正则表达式匹配以 D 开头的值之后)
D1
D2
D99
第三个列表框: > (在使用正则表达式匹配以 C 开头的值之后)
C9
C130
并且在这些值与 SECOND 和 THIRD 列表框匹配之后,
新主列表框将如下所示:
R7
R1
Q9
代码
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, @"^.+(0402)");
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;
}
// Finds the placement 2 package types.
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 CHOICE 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;
}
问题
我目前有正则表达式,可以对文件进行排序并将文件分成三个单独的列表框。但是...我宁愿将数据放置在其中一个列表框中,然后进行排序/删除如果它与正确的正则表达式匹配,则从该列表框中获取。
- 如何将 .txt 文档加载到列表框中(每个新行都是列表框中的一个新项目),然后使用正则表达式遍历主列表框中的每一行以确定这些行是否进入不同的第二个或第三个列表框还是留在主列表框中?
I have a file with data in it that I would like to split up into three different ListBoxes. I want the first ListBox to contain all of the data (a new list item for every line of data read from the text document).
Main ListBox:
D1
D2
C9
R7
R1
C130
D99
Q9
Second ListBox: (after a regex was used to match values that start with D)
D1
D2
D99
Third ListBox: (after a regex was used to match values that start with C)
C9
C130
And after those values were matched for the SECOND and THIRD ListBoxes, the
New Main ListBox will look like this:
R7
R1
Q9
CODE
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, @"^.+(0402)");
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;
}
// Finds the placement 2 package types.
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 CHOICE 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;
}
QUESTIONS
I currently have my regular expressions that sort through and divide the file up into three seperate ListBoxes.. However... I would rather have the data placed in one of the ListBoxes and then sorted/removed from that ListBox if it matches the proper regular expression.
- How do I go about loading the .txt document into the ListBox (with every new line being a new item in the ListBox) and then going through each line in the main ListBox with regex to determine if the lines go into a different second or third ListBox or stay in the main ListBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将它们组合成一个,而不是使用多个正则表达式。
您也不需要先调用
Regex.Match
,然后调用match.Success
;只需调用Regex.IsMatch即可。我不明白您为什么使用
result.Remove(line)
。您从未添加过该不匹配的行,那么为什么需要删除它呢?最后,我认为您不需要使该方法特定于使用
List
。您可以通过指定IEnumerable
使其更加通用。我重写了第一个来演示原理:
如果您确实需要一个新的
List
,您只需在调用后添加.ToList()
findPackagePlacementOneType
。Instead of multiple regexes you could just combine them into one.
You also don’t need to call
Regex.Match
followed bymatch.Success
; just callRegex.IsMatch
.I don’t understand why you are using
result.Remove(line)
. You never added that non-matching line, so why do you need to remove it?Finally, I don’t think you need to make the method specific to using
List<string>
. You can make it more general by specifyingIEnumerable<string>
.I’ve rewritten the first one to demonstrate the principle:
If you ever really need a new
List<string>
, you can just add.ToList()
after a call tofindPackagePlacementOneType
.我建议创建一些
ObservableCollection
,每个ListBox
一个。ObservableCollection
在添加或删除项目时引发事件,这允许ListBox
等内容在发生更改时自动更新。假设它们的名称为items1
、2
和3
。从items1 = new ObservableCollection(list);
开始,另一个初始化为空集合。另外,修改您发布的方法,以获取单个字符串并返回一个布尔值,说明它是否匹配任何字符串。第 1 部分,将字符串集合放入
ListBox
es:第 2 部分,遍历列表中的每一项:
.ToArray() 是 LINQ 扩展;它是必需的,因为您无法修改正在迭代的枚举。
第 3 部分:此外,我建议根据“不要重复自己”(DRY) 原则对这些方法进行一些修改:
这还有一个好处是不需要评估数十个最终根本不会被读取的正则表达式。
I'd recommend making a few
ObservableCollection<string>
s, one for each of yourListBox
es.ObservableCollection
s raise events when items are added or removed, which allows things likeListBox
es to automatically update when they change. Say they're calleditems1
,2
, and3
. Start withitems1 = new ObservableCollection<string>(list);
, and the other initialized as empty collections. Also, rework the methods you posted to take a singlestring
and return abool
saying whether it matched any.Part 1, putting your collections of strings into the
ListBox
es:Part 2, going through each item in the list:
The .ToArray() is the LINQ extension; it's needed because you can't modify an enumerable that you're iterating through.
Part 3: Also, I'd recommend reworking the methods a bit, based on the Do not Repeat Yourself (DRY) principle:
This also has the benefit of not needing to evaluate dozens of regex's that don't end up being read at all.