正则表达式不是有效的类型或命名空间C#
我正在尝试将此代码段添加到我的代码中:正则表达式不是有效的类型或命名空间C#,c#,regex,C#,Regex,我正在尝试将此代码段添加到我的代码中: public string Highlight(string InputTxt) { string Search_Str = txtSearch.Text.ToString(); // Setup the regular expression and add the Or operator. Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOpt
public string Highlight(string InputTxt)
{
string Search_Str = txtSearch.Text.ToString();
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
// Set the RegExp to null.
RegExp = null;
}
但是,由于某些原因,“Regex”没有显示-找不到类型或命名空间。我想我一定是在使用更新版本的C#-有人能帮我找到更新的方法吗?我正在使用System.Text.RegularExpressions.Regex-也许他们完全摆脱了它
using System.Text.RegularExpressions;
试试这个名称空间
我正在使用System.Text.RegularExpressions.Regex
确保在using指令中只引用命名空间,而不引用类:
using System.Text.RegularExpressions;
你错过了PCAE
using System.Text.RegularExpressions;
您确定使用System.Text.RegularExpressions时有
代码>?您没有在方法体中完全限定System.Text.RegularExpressions.Regex
,因此您必须在那里使用,对吗?您是否在类的顶部添加了using System.Text.RegularExpressions
?顺便说一句,代码中不需要设置RegExp=null。作为旁注:建议(a)使用缩进,(b)以小写字母(regExp
,而不是regExp
;inputxt
,searchStr
)开始局部变量。这使得阅读代码更加容易。当您在编写代码几个月后再次阅读代码时,您会对此感激不尽。哦,还有(c),不要使用无用的ToString()
s。我很确定txtSearch.Text
已经是string
类型。因为这个问题已经用这个精确的解决方案得到了回答,所以这个答案不提供任何额外的信息来解决它。当你能为提问者提供新信息或新见解时,最好发布答案。