Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我会得到;名称Regex在当前上下文中不存在;从我的C代码?_C#_Regex - Fatal编程技术网

C# 为什么我会得到;名称Regex在当前上下文中不存在;从我的C代码?

C# 为什么我会得到;名称Regex在当前上下文中不存在;从我的C代码?,c#,regex,C#,Regex,为什么我会出现这个错误: 当前上下文中不存在名称Regex 从我的代码 if (Regex.IsMatch(string1, @"^[a-zA-Z]+$")) 确保引用了System.Text.RegularExpressions命名空间。添加 using System.Text.RegularExpressions; 到类文件的顶部。您需要包含正确的命名空间才能访问Regex类: using System.Text.RegularExpressions; 程序中不存在Regex类。但是,

为什么我会出现这个错误:

当前上下文中不存在名称Regex

从我的代码

if (Regex.IsMatch(string1, @"^[a-zA-Z]+$"))

确保引用了
System.Text.RegularExpressions
命名空间。

添加

using System.Text.RegularExpressions;

到类文件的顶部。

您需要包含正确的命名空间才能访问
Regex
类:

using System.Text.RegularExpressions;

程序中不存在
Regex
类。但是,如果您从某个外部库引用它,您将能够在程序中使用它


要能够使用
Regex
类及其所有功能,请将
System.Text.RegularExpressions
名称空间添加到代码中。

如果已包含“正在使用”但仍然没有运气,请先实例化它

string regexPattern = @"^[a-zA-Z]+$";    
Regex r = new Regex(regexPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(string1);
if(m.Success)
{
   // Win!
}

确保键入的
Regex.IsMatch
,而不是
Regex.IsMatch
,大写字母E。我以前就弄错了。

将导入添加到cs文件的顶部?。是否向项目中添加了对
System.Text.RegularExpressions
的引用以及对代码的using指令?(
使用System.Text.RegularExpressions
)?