Geeks With Blogs
Michael Holland
A good majority of my work is for mobile applications using the Compact Framework. Something really annoying I came across today was that if I used a variable for the pattern in my Regex then the IsMatch did not work, but if I used a constant then it did.

So, for example, if I used:
string val = "YELLOW";
string pattern = "(YELLOW)$";

The following statement does not give the correct result (true):
bool flg1 = Regex.IsMatch("YELLOW", pattern);

But the following does:
bool flg2 = Regex.IsMatch(val, "(YELLOW)$");


So far I have not found a workaround (e.g. creating a Regex object and trying to use IsMatch using it also does not work if the pattern is from a var), but am investigating... if anybody has a solution then please let me know...

26 March 2009 - Okay, a colleague has finally helped me out with this one. There is a workaround so instead of using the IsMatch, rather return a Match object and then use the Success property to determine if the match was successful. Thanks Rich!

Solution below:

Regex r = new Regex(this.txtPattern.Text), RegexOptions.IgnoreCase);
bool pass = r.Match(this.txtInput.Text).Success;
this.txtFeedback.Text = (pass ? "Matched" : "No match");
Posted on Friday, February 27, 2009 7:45 AM | Back to top


Comments on this post: Regex.IsMatch for Compact Framework

# re: Regex.IsMatch for Compact Framework
Requesting Gravatar...
It was me! Www.binaryrefinery.com
Left by Richard Jones on Mar 26, 2009 1:45 PM

Your comment:
 (will show your gravatar)


Copyright © LEV | Powered by: GeeksWithBlogs.net