Notes about Regex::IsMatch


if (!System::Text::RegularExpressions::Regex::IsMatch (this->MyString->Text, "[0-9a-fA-f]{1,3}" ))
   MessageBox::Show ( "Invalid value" );

The problem with the above expression is that it doesn’t match what it should. There are 2 special characters, ^ and $ which specify the beginning and the end of the string to match. The way you used it, it matches any of the characters in the string. If you have a valid character, it will ignore the rest. Now, if you add an ending $, the expression will not accept a string that doesn’t end in a valid (or its understanding of “valid”) character. If you will add the ^ in front, only 3 character hexadecimal strings will pass the filter. BTW, I believe you meant to use “[0-9a-fA-F]{1,3}”, not f. This way, even W will match, because it is between A and f.
Try the following and observe the differences:


if (!System::Text::RegularExpressions::Regex::IsMatch
	//(this->myTextBox->Text, "[0-9a-fA-F]{1,3}" ))
	//(this->myTextBox->Text, "^[0-9a-fA-F]{1,3}" ))
	//(this->myTextBox->Text, "[0-9a-fA-F]{1,3}$" ))
	(this->myTextBox->Text, "^[0-9a-fA-F]{1,3}$" ))
{
	MessageBox::Show ("Address 2: Invalid value");

The use of enclosing ^ and $ tokens to indicate that the entire string, not just a substring, must match the regular expression.

Regular expression examples can be found in help at: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_fxfund/html/e9fd53f2-ed56-4b09-b2ea-e9bc9d65e6d6.htm

A good resource for expressions: http://www.regular-expressions.info/reference.html

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.