Zebra0.com

csharp strings

Sets of Characters

Brackets [] enclose a set of characters. [abc] is the set of letters "a", "b", "c". [A-Z] is the set of upper case letters. [a-z] is the set of lower case letters, [0-9] is the set of digits.

Special characters:

[\^$.|?*+()

If you want to include any of these special characters in a set you must use a backslash in front of them. The sentence should end with [\.\?!] that is, a period, a question mark or an exclamation point. The expression below says that a sentence can have any number (indicated by the *) of the set A-Z, a-z and a space followed by a period, ? or ! Notice that there is a backslash in front of the special characters period and question mark but not in front of the exclamation point.

In C#, putting @ before a string means that characters are as is, with out special meaning.
It marks the string as a verbatim string literal - anything in the string that would normally be interpreted as an escape sequence, such as \ is ignored.

private void btnOK_Click(object sender, EventArgs e)
{
   String s = txtInput.Text;
   Regex regx= new Regex(@"[A-Za-z ]*[\.\?!]");
   Match m = regx.Match(s);
   if (m.Value==s) lblOutput.Text = "OK";
   else lblOutput.Text = "Not valid";
}

This would not accept "Hello, how are you?" because of the comma. Furthermore, a sentence should start with an upper case letter. Experiment to see if you can add that.

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.

End of lesson, Next lesson: Files in C#