我正在尝试检查C#,例如:
如果 name_Ford_value_Focus (好)
或
name_value_Focus (糟糕)
符合模板
“的名_ {0} _value {1} ”
我必须使用正则表达式吗?
答案 0 :(得分:2)
假设您要匹配完整的字符串(如果没有,则从模式中删除^和$以匹配字符串)...
class Program
{
static void Main()
{
string pattern = @"^name_.+_value_.+$";
Console.WriteLine( Regex.Match( "name_Ford_value_Focus", pattern ).Success.ToString() );//true
Console.WriteLine( Regex.Match( "name_value_Focus", pattern ).Success.ToString() );//false
//Other examples:
Console.WriteLine( Regex.Match( "name_Toyota_value_Corolla", pattern ).Success.ToString() );//true
Console.WriteLine( Regex.Match( "name_Mini_value_", pattern ).Success.ToString() );//false
Console.WriteLine( Regex.Match( "Applename_Ford_value_FocusApple", pattern ).Success.ToString() );//false because full string match. Remove ^ and $ from pattern for true
}
}
其中: