Could someone tell me why I can't use the following code? It kept saying 'string' does not contain a definition for 'Match' and no extension method 'Match'.

string SearchText = "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false";
string Regex = @"\btrue\b";
int NumberOfTrues = Regex.Match(SearchText, Regex).Count;

Recommended Answers

All 3 Replies

string SearchText = "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false";
string Regex = @"\btrue\b";
int NumberOfTrues = Regex.Match(SearchText, Regex).Count;

stirng doesn't contain a Match() extention method so you can't do Regex.Match().

take a look at this Link it might help you

Minor change, big difference:

using System.Text.RegularExpressions;

namespace DW_420515_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         string SearchText = "7,true,NA,false:67,false,NA,false:5,false,NA,false:5,false,NA,false";
         string strRx = @"\btrue\b";
         int NumberOfTrues = Regex.Matches(SearchText, strRx).Count;
      }
   }
}

why did you name your string varaible as "Regex"? You cannot do that, sice Regex is a reserved word of C#.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.