[英]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.