如何在正则表达式列表中找到与我的输入匹配的第一个正则表达式?

时间:2012-01-13 21:33:19

标签: c# regex

还有其他方法可以写下以下内容吗?

string input;

var match = Regex.Match(input, @"Type1");

if (!match.Success)
{
  match = Regex.Match(input, @"Type2");
}

if (!match.Success)
{
  match = Regex.Match(input, @"Type3");
}

基本上,我想通过一系列表达来运行我的字符串,看看哪一个坚持。

3 个答案:

答案 0 :(得分:11)

var patterns = new[] { "Type1", "Type2", "Type3" };
Match match;
foreach (string pattern in patterns)
{
    match = Regex.Match(input, pattern);
    if (match.Success)
        break;
}

var patterns = new[] { "Type1", "Type2", "Type3" };
var match = patterns
    .Select(p => Regex.Match(input, p))
    .FirstOrDefault(m => m.Success);

// In your original example, match will be the last match if all are
// unsuccessful. I expect this is an accident, but if you want this
// behavior, you can do this instead:
var match = patterns
    .Select(p => Regex.Match(input, p))
    .FirstOrDefault(m => m.Success)
    ?? Regex.Match(input, patterns[patterns.Length - 1]);

由于LINQ to Objects使用延迟执行,Regex.Match只会在找到匹配项之前被调用,因此您不必担心此方法过于急切。

答案 1 :(得分:5)

是的,我会这样写,以避免多次执行Regex匹配:

        match = Regex.Match(input, @"Type1|Type2|Type3");

        if (match.Success)
        {
            // loop, in case you are matching to multiple occurrences within the input.
            // However, Regex.Match(string, string) will only match to the first occurrence.
            foreach (Capture capture in match.Captures)
            {
                // if you care to determine which one (Type1, Type2, or Type3) each capture is
                switch (capture.Value)
                {
                    case "Type1":
                        // ...
                        break;
                    case "Type2":
                        // ...
                        break;
                    case "Type3":
                        // ...
                        break;
                }
            }
        }

或者,如果您要检查任意模式列表:

        // assumption is that patterns contains a list of valid Regex expressions
        match = Regex.Match(input, string.Join("|", patterns));

        if (match.Success)
        {
            // obviously, only one of these return statements is needed

            // return the first occurrence
            return match.Captures[0].Value;

            // return an IEnumerable<string> of the matched patterns
            return match.Captures.OfType<Capture>().Select(capture => capture.Value);
        }

这是另一种使用命名捕获组的方法,以便为每个模式编制索引。当找到匹配时,我们尝试确定哪个捕获组匹配。

由于“模式”与索引的重复不必要的连接,我非常不喜欢这段代码,但我不知道如何做到这个更干净:

编辑:我使用字典

清理了这段代码
        // assumption is that patterns contains a list of valid Regex expressions
        int i = 0;
        var mapOfGroupNameToPattern = patterns.ToDictionary(pattern => "Pattern" + (i++));

        match = Regex.Match(input, string.Join("|", mapOfGroupNameToPattern.Select(x => "(?<" + x.Key + ">" + x.Value + ")")));

        if (match.Success)
        {
            foreach (var pattern in mapOfGroupNameToPattern)
            {
                if (match.Groups[pattern.Key].Captures.Count > 0)
                {
                    // this is the pattern that was matched
                    return pattern.Value;
                }
            }
        }

答案 2 :(得分:0)

另一种方法。它迭代遍历所有列表,但是你可以查看一个或多个可变数字或字符串来匹配,而不必编写x num的if语句。

string input = "Type1";
List<string> stringsToTest = new List<string>() { @"Type1", @"Type2", @"Type3" };

var q = from string t in stringsToTest
        where Regex.IsMatch(input, t)
        select t;

//This way you can get how many strings on the list matched the input   
foreach(string s in q)
{
    Console.WriteLine(s);
}