Search This Blog

Thursday, November 7, 2013

Regex IsMatch method

C# > Text > RegularExpressions > IsMatch

Regex IsMatch method indicates whether the regular expression specified finds a match in a specified input string.

Example:

string[] partNumbers = { "1298-6736","343434" };
Regex rgx = new Regex(@"^[a-zA-Z0-9-]*-[a-zA-Z0-9]*$");

foreach (string partNumber in partNumbers)
   if (rgx.IsMatch(partNumber))
     MessageBox.Show  (partNumber);

Pattern

^                      begin the match at the beginning of the line.
[a-zA-Z0-9]    match a single alphabetic character (a through z or A through Z) or numeric character.
-                      match a hyphen
*                      must be zero or more of these characters
$                      end the match at the end of the line