【问题标题】:C# .contains() using Regular ExpressionsC# .contains() 使用正则表达式
【发布时间】:2016-11-01 13:44:54
【问题描述】:

我目前正在尝试执行数组搜索,如果数组包含与特定字符匹配的字符串,则返回 true。

例如:

我有一个包含这 3 个值的数组

"Atlanta Hawks are great this year!",
"Atlanta Braves are okay this year!",
"Atlanta Falcons are great this year!"

如果数组包含至少一个与下面的针匹配的值,我要做的是返回 true...

"Atlanta* *are great this year!"

(在本例中决定使用星号作为通配符)

在这种情况下会返回 true。

但是对于这个数组

"Atlanta Hawks are bad this year!",
"Detroit Tigers are okay this year!",
"New England Patriots are good this year!"

它会返回 false。

目前我正在做的是..(而不是工作)

 if (result.Properties["memberOf"].Matches("Atlanta* *are great this year!"))
                {
                    return true;
                }

我的问题是,是否有任何通配符可以与 .Contains() 方法一起使用,或者是否有与 C# 中 PHP 中的 preg_grep() 类似的方法?

提前感谢您的帮助。

【问题讨论】:

  • 您必须使用Regex 类才能使用正则表达式。 if (Regex.Matches(result.Properties["memberOf"], "Atlanta...."))
  • 您将使用IsMatch() 方法获得boolean 结果。还要记住,正则表达式使用.* 表示“任意数量的任意字符”

标签: c# arrays regex


【解决方案1】:

我建议将通配符(带有*?)转换成正则表达式模式,然后使用Linq查找如果有匹配正则表达式的Any 项:

  string[] data = new string[] {
    "Atlanta Hawks are great this year!",
    "Atlanta Braves are okay this year!",
    "Atlanta Falcons are great this year!"
  };

  string wildCard = "Atlanta* *are great this year!";

  // * - match any symbol any times
  // ? - match any symbol just once
  string pattern = Regex.Escape(wildCard).Replace(@"\*", ".*").Replace(@"\?", ".");

  bool result = data.Any(item => Regex.IsMatch(item, pattern));

您可能希望将实现包装到一个方法中:

  private static bool ContainsWildCard(IEnumerable<String> data, String wildCard) {  
    string pattern = Regex.Escape(wildCard).Replace(@"\*", ".*").Replace(@"\?", ".");

    return data.Any(item => Regex.IsMatch(item, pattern));
  }

  ...

  String[] test = new String[] {
    "Atlanta Hawks are bad this year!",
    "Detroit Tigers are okay this year!",
    "New England Patriots are good this year!"
  }

  bool result = ContainsWildCard(test, "Atlanta* *are great this year!");

【讨论】:

    【解决方案2】:

    你可以这样做:

    string[] input = {"Atlanta Hawks are great this year!", "Atlanta Braves are okay this year!", "Atlanta Falcons are great this year!"};
    var output = false;
    Regex reg = new Regex("Atlanta (.*) are great this year!");
    foreach (var item in input)
    {        
        Match match = reg.Match(item);
        if (match.Success)
        {
            output = true;
            break;
        }
    }
    

    【讨论】:

    • 对于这个小循环没有关系,但通常最好避免在循环内初始化new Regex。最好声明一次并重复使用。
    • 找到匹配项后是否有理由继续检查?您可能应该在 match.Success 上中断/返回。
    【解决方案3】:

    手头没有编译器,但应该是这样的:

    public boolean checkMyArray(string[] theStringArray) {
        string pattern = "Atlanta (.*) are (great|okay) this year!";
        foreach (string s in theStringArray) {
           if(System.Text.RegularExpressions.Regex.IsMatch(s, pattern))
              return true;
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多