2

I have a string looking something like this

string myString = "Master Language=\"C#\" MasterPageFile=\"~/masterpages/Libraries.master\"";
  1. I need to verify that it contain the exact words Master and Language="C#"
  2. I cannot always guarantee that the words Master and Language will be placed like this, hence stuff like Contains("Master Language") wont do

I've been playing around with regex.IsMatch without any results for a while so if anyone could be able to help me I'd appreciate that!

default
  • 11,485
  • 9
  • 66
  • 102
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • 1
    Why not `IndexOf() >= 0` or `Contains`? – vcsjones May 01 '12 at 16:30
  • Contains will get both Master and MasterPageFile so nope nope! – Eric Herlitz May 01 '12 at 16:38
  • @Trikks that's only true if you're searching for "Master" but if you search for `Master Language="C#"` as it appears in your example, it won't match "MasterPage". Unless those two words can appear anywhere in the string, not after one another as shown, then `Contains` would suffice. – Ahmad Mageed May 01 '12 at 16:41
  • I cannot always tell that the string always is constructed that way. The attributes may be shifted sometimes! – Eric Herlitz May 01 '12 at 16:42
  • @Trikks You needed to include that in your question, then. – vcsjones May 01 '12 at 16:47

6 Answers6

3

Since you need to find the occurrences of the word in any order, you can use the following pattern:

string pattern = @"^(?=.*\bMaster\b)(?=.*Language=""C#"").+$";

This uses positive look-arounds to check for the existence of Master and Language="C#". Notice the use of the word-boundary meta-character, \b, which ensures that "Master" is an exact match. That ensures that a partial match in "MasterPage" won't occur.

Example:

string[] inputs = 
{
    "Master Language=\"C#\" MasterPageFile=\"~/masterpages/Libraries.master\"", // true
    "Language=\"C#\" MasterPageFile=\"~/masterpages/Libraries.master\" Master", // true
    "Language=\"C#\" MasterPageFile=\"~/masterpages/Libraries.master\"" // false
};

string pattern = @"^(?=.*\bMaster\b)(?=.*Language=""C#"").+$";

foreach (var input in inputs)
{
    Console.WriteLine(Regex.IsMatch(input, pattern));
}
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • That worked well, I'm gonna go with a slightly different approach but this is as close as it gets. Thanks for understanding the value of solving issues like this with Regex! – Eric Herlitz May 01 '12 at 17:00
0

You can use Contains() method of string class.

Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
  • Well no, not since Im looking for Master when the word MasterPageFile also may be in the string. That method will always return true if either one of them is in the string and thats not good. – Eric Herlitz May 01 '12 at 16:37
0

You could find out if the pattern exists in the string by using the IndexOf Method, LINK.

bool found = myString.IndexOf("Master Language=\"C#\"") != -1;
dmportella
  • 4,614
  • 1
  • 27
  • 44
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • Would do the trick but I cannot always tell that the string always is constructed that way. The attributes may be shifted sometimes! – Eric Herlitz May 01 '12 at 16:39
0
string strTestMe = Regex.Replace(myString, ".*(Master Language=\"C#\").*", "$2")
If strTestMe <> ""
  DO STUFF
End If
RJ Cuthbertson
  • 1,458
  • 1
  • 20
  • 36
0

I realize this might not be the answer you're seeking but honestly it seems like overkill to use regex here. You may get better performance overall by simply using string.Contains

-2

This is what I did

bool containMaster = Regex.IsMatch(myString, @"\bMaster\b");
bool containLanguage = Regex.IsMatch(myString, "Language=\"C#\"");

Simple and effective

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157