Basic Regular Expression in C#
Objective
In some case you want to check whether or not a string is conform to an template. For example, you want to check if a given string is only created by numeric character from 0-9. Another example is checking valid email format. In these cases, C# language provides you an easy but quick method for doing this.
Let's go
Of course you can check format of string by your way you want. You can write your code for checking the format of a given string. But in case of checking a complex string, the work might take much time from you and errors could happen. To solve this problem, C# provides a very helpful way to do. This called Regular Expression. You just make a expression that conform a rule of C# - here C# calls regular expression, and using an defined class Regex of C# to check whether the input string is in the form of expression. For the purpose of this tutorial, we just show you the basic of Regular expression. If you want to know the all things from this theme, you can find in MSDN Library.
Common Regular Expression Characters
You use the Common Regular Expression Character to build the regular expression string. Table bellow list common regular expression characters that usually used in program:
Metacharacter | Description |
. | Matches any single character except newline (\n). |
[] | Matches a single character contained within the brackets. A range of characters can be specified using the – character. |
[^ ] | Matches a single character not contained with the brackets. |
^ | Indicates matching should start at the beginning of the line. |
$ | Indicates matching should end at the end of the line. |
\w | Matches a word character. This is equivalent to [a–zA–Z_0–9]. |
\W | Matches a nonword character. |
\s | Matches a space character. This is equivalent to [\n\r\t\f]. |
\S | Matches a nonspace character. |
\d | Matches a decimal digit. This is equivalent to [0–9]. |
\D | Matches a nondigit. |
\uxxxx | Unicode escape sequence for a character with a hexadecimal value xxxx. Also a variable-length version can contain 1 to 4 numeric values. |
* | Matches the preceding element zero or more times. |
+ | Matches the preceding element one or more times. |
? | Matches the preceding element zero or one times. |
{n} | Matches the preceding element exactly n times. |
{n,} | Matches the preceding element at least n times. |
{n,m} | Matches the preceding element at least n times but no more than m times. |
| | (Alternation operator) Matches the expression either before or after the operator. |
( ) | Defines an unnamed capturing group. |
(? |
Defines a named capturing group. |
(? |
Defines a numbered capturing group. |
Using Regex class
Regex class provides both static and instance member. For easy using, we use the static member. Let see the example as follows:
- /*
- * this function checks whether an input string (email) is in right format of email.
- * if input string in email format so it returns true, otherwise it returns false.
- */
- private Boolean checkMatchString(String email)
- {
- //regular expression for checking - we also call pattern
- string pattern = "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
- //using Regex class with IsMatch member to check
- if (System.Text.RegularExpressions.Regex.IsMatch(email, pattern))
- {
- return true;
- }
- return false;
- }
- static void Main(string[] args)
- {
- //declare two email for checking format;
- String second_email = "test123";
- //check for two email
- if (my_check.checkMatchString(firt_email))
- {
- Console.WriteLine("email " + firt_email + " is in the right format!");
- }else{
- Console.WriteLine("email " + firt_email + " is in the wrong format!");
- }
- if (my_check.checkMatchString(second_email))
- {
- Console.WriteLine("email " + second_email + " is in the right format!");
- }
- else
- {
- Console.WriteLine("email " + second_email + " is in the wrong format!");
- }
- Console.ReadKey();
- }
Bellow is the output of the program:
email [email protected] is in the right format!
email test123 is in the wrong format!
Here the first email is in right format, and the second is a wrong format of email. You see that the program is very simple, but efficient.
Summary
In this tutorial we have show you about regular expression and how to use it to check the format of an string. You can use regular expression to check many things that you want. Just declare an pattern, and check.
Add new comment
- 117 views