【发布时间】: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结果。还要记住,正则表达式使用.*表示“任意数量的任意字符”