[英]Finding Patterns in Lists of Strings and Identifying Groups
我正在寻找在不知道确切字符串的情况下识别组的方法。 字符串在列表之间可能有所不同,但是通过查看模式可以明显看出模式的重复。 我从未使用过REGEX表达式,但只是没有开始使用它们,我觉得这比看起来要难。
1区
区域1模块A
区域1模块B
Zone1ModuleAWheel1
Zone1ModuleAWheel2
Zone1ModuleBWheel1
Zone1ModuleBWheel2
2区
Zone2ModuleA
Zone2ModuleB
Zone2ModuleAWheel1
Zone2ModuleAWheel2
Zone2ModuleBWheel1
Zone2ModuleBWheel2
该列表将包含这些模式的更大列表。 这些名称将来可能会更改,所以我希望能够识别该模式。 最终结果将匹配所有Zone , ModuleA , ModuleB , ModuleAWheel1 ...等。 我正在研究REGEX教程,希望对您有所帮助! 谢谢
我不知道,如果这是您想要实现的目标,则可以尝试以下操作:
void Main()
{
var regex = new Regex(@"[A-Z][^A-Z]*[AB]?");
var lines = linesInFile
.Replace("\\r", "")
.Split(new[] { '\n' })
.Where(i => !string.IsNullOrEmpty(i));
var listOfTokens = new List<string>();
foreach (var line in lines)
{
foreach (Match match in regex.Matches(line))
{
var value = match.Value;
if (!listOfTokens.Contains(value))
{
listOfTokens.Add(value);
}
}
}
listOfTokens.Dump();
}
// Define other methods and classes here
private string linesInFile = @"Zone1
Zone1ModuleA
Zone1ModuleB
Zone1ModuleAWheel1
Zone1ModuleAWheel2
Zone1ModuleBWheel1
Zone1ModuleBWheel2
Zone2
Zone2ModuleA
Zone2ModuleB
Zone2ModuleAWheel1
Zone2ModuleAWheel2
Zone2ModuleBWheel1
Zone2ModuleBWheel2";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.