简体   繁体   English

在字符串列表和标识组中查找模式

[英]Finding Patterns in Lists of Strings and Identifying Groups

I am looking for ways to identify groups without knowing the exact strings. 我正在寻找在不知道确切字符串的情况下识别组的方法。 The strings may differ from list to list but the repetition of the pattern is obvious by looking at it. 字符串在列表之间可能有所不同,但是通过查看模式可以明显看出模式的重复。 I have never used REGEX expressions but just not starting to use them, I feel this is harder than it may seem. 我从未使用过REGEX表达式,但只是没有开始使用它们,我觉得这比看起来要难。

Zone1 1区

Zone1ModuleA 区域1模块A

Zone1ModuleB 区域1模块B

Zone1ModuleAWheel1 Zone1ModuleAWheel1

Zone1ModuleAWheel2 Zone1ModuleAWheel2

Zone1ModuleBWheel1 Zone1ModuleBWheel1

Zone1ModuleBWheel2 Zone1ModuleBWheel2

Zone2 2区

Zone2ModuleA Zone2ModuleA

Zone2ModuleB Zone2ModuleB

Zone2ModuleAWheel1 Zone2ModuleAWheel1

Zone2ModuleAWheel2 Zone2ModuleAWheel2

Zone2ModuleBWheel1 Zone2ModuleBWheel1

Zone2ModuleBWheel2 Zone2ModuleBWheel2

The list will contain a much larger list of these patterns. 该列表将包含这些模式的更大列表。 These names may change in the future so i want to be able to recognize the pattern. 这些名称将来可能会更改,所以我希望能够识别该模式。 The end result would match all the Zone , ModuleA , ModuleB , ModuleAWheel1 ...so on. 最终结果将匹配所有ZoneModuleAModuleBModuleAWheel1 ...等。 I am digging through REGEX tutorials and would appreciate any help! 我正在研究REGEX教程,希望对您有所帮助! Thanks 谢谢

i do not know, if this is what you want to achive, you could try this: 我不知道,如果这是您想要实现的目标,则可以尝试以下操作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM