6

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Hani Honey
  • 2,101
  • 11
  • 48
  • 76
  • 2
    Are you sure you have `using System.Text.RegularExpressions;`? You don't fully qualify `System.Text.RegularExpressions.Regex` within your method body, so you must have the using up there, right? – BoltClock Sep 15 '11 at 13:37
  • Did you add `using System.Text.RegularExpressions` at the top of your class? – Icarus Sep 15 '11 at 13:38
  • btw there is no need to set RegExp = null in your code. http://stackoverflow.com/questions/2785/setting-objects-to-null-nothing-after-use-in-net – Peter Kelly Sep 15 '11 at 13:39
  • 1
    As a side-note: It is recommended to (a) use indentation, and (b) start local variables with a lowercase letter (`regExp`, not `RegExp`; `inputTxt`, `searchStr`). That makes reading your code much easier. You'll be grateful for it when you read your code again a few month after you've written it. Oh, and (c), don't use useless `ToString()`s. I'm pretty sure that `txtSearch.Text` is already of type `string`. – Heinzi Sep 15 '11 at 13:40

3 Answers3

24
using System.Text.RegularExpressions;

Try that namespace.

AD.Net
  • 13,352
  • 2
  • 28
  • 47
4

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;
Jay
  • 56,361
  • 10
  • 99
  • 123
-5

you are missing namespcae.

using System.Text.RegularExpressions;
Firoz Khan
  • 623
  • 1
  • 9
  • 23
  • 5
    Since this question has already been answered with this exact solution, this answer doesn't offer any additional information to solving it. It's best to post an answer when you can offer new information or a new insight for the question asker. – SuperBiasedMan Jul 22 '15 at 22:08