Zebra0.com

csharp strings

Robots

As we all know, robot names have an upper case letter followed by a digit, then another upper case letter followed by a digit,

Examples: R2D2, C3P0.

Quantity Specifier

The set of characters is followed by one of the following to indicate how many characters in that set are allowed.

? 0 or 1 time
* 0 or more
+ 1 or more times
{n} n must be an integer where n>=1 item must appear exactly n times.
{n,m} n must be an integer where n>=1, item must appear n to m times.
{n,} where n >= 0, item must appear n or more times.

So what is the regular expression to determine if a string is a valid robot name? answer

private void btnOK_Click(object sender, EventArgs e)
{
   String s = txtInput.Text;
   Regex robotregx= new Regex(@"([A-Z][0-9]){2}");
   Match m = robotregx.Match(s);
   if (m.Value==s) lblOutput.Text = "OK";
   else lblOutput.Text = "Not valid";
}

End of lesson, Next lesson: Files in C#