Blog Stats
  • Posts - 10
  • Articles - 0
  • Comments - 1
  • Trackbacks - 0

 

Regex.IsMatch for Compact Framework

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");
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Regex.IsMatch for Compact Framework

Gravatar It was me! Www.binaryrefinery.com 3/26/2009 1:45 PM | Richard Jones

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © LEV