Loading

Basic Regular Expression in C#

Submitted by: 
Language: 

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:

  1. /*
  2.  * this function checks whether an input string (email) is in right format of email.
  3.  * if input string in email format so it returns true, otherwise it returns false.
  4. */
  5. private Boolean checkMatchString(String email)
  6. {
  7. //regular expression for checking - we also call pattern
  8. 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})$";
  9.  
  10. //using Regex class with IsMatch member to check
  11. if (System.Text.RegularExpressions.Regex.IsMatch(email, pattern))
  12. {
  13. return true;
  14. }
  15. return false;
  16. }
  17.  
  18. static void Main(string[] args)
  19. {
  20. MyRegularEpression my_check = new MyRegularEpression();
  21.  
  22. //declare two email for checking format;
  23. String firt_email = "<a href="mailto:[email protected].com" rel="nofollow">[email protected]</a>";
  24. String second_email = "test123";
  25.  
  26. //check for two email
  27. if (my_check.checkMatchString(firt_email))
  28. {
  29. Console.WriteLine("email " + firt_email + " is in the right format!");
  30. }else{
  31. Console.WriteLine("email " + firt_email + " is in the wrong format!");
  32. }
  33. if (my_check.checkMatchString(second_email))
  34. {
  35. Console.WriteLine("email " + second_email + " is in the right format!");
  36. }
  37. else
  38. {
  39. Console.WriteLine("email " + second_email + " is in the wrong format!");
  40. }
  41.  
  42. Console.ReadKey();
  43. }

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.