Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex is not a valid type or namespace C#

Tags:

c#

regex

I'm trying to add this snippet to my code:

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;
}

However, for some reason, "Regex" is not showing up - the type or namespace is not found. I suppose I must be using a newer version of C# - can anybody help me out with the newer way to do this? I AM using System.Text.RegularExpressions.Regex - maybe they got rid of it entirely?

like image 983
Hani Honey Avatar asked Sep 15 '11 13:09

Hani Honey


2 Answers

using System.Text.RegularExpressions;

Try that namespace.

like image 149
AD.Net Avatar answered Oct 22 '22 08:10

AD.Net


I AM using System.Text.RegularExpressions.Regex

Make sure that in your using directive, you only reference the namespace, not the class:

using System.Text.RegularExpressions;
like image 24
Jay Avatar answered Oct 22 '22 08:10

Jay