• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Basic Regular Expression in C#

dagwood

Microservice Architect
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
35
Likes
184
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. //using Regex class with IsMatch member to check
  10. if

    (

    System.Text

    .

    RegularExpressions

    .

    Regex

    .

    IsMatch

    (

    email, pattern)

    )
  11. {
  12. return

    true

    ;
  13. }
  14. return

    false

    ;
  15. }

  16. static

    void

    Main(

    string

    [

    ]

    args)
  17. {
  18. MyRegularEpression my_check =

    new

    MyRegularEpression(

    )

    ;

  19. //declare two email for checking format;
  20. String

    firt_email =

    "[email protected]"

    ;
  21. String

    second_email =

    "test123"

    ;

  22. //check for two email
  23. if

    (

    my_check.

    checkMatchString

    (

    firt_email)

    )
  24. {
  25. Console.

    WriteLine

    (

    "email "

    +

    firt_email +

    " is in the right format!"

    )

    ;
  26. }

    else

    {
  27. Console.

    WriteLine

    (

    "email "

    +

    firt_email +

    " is in the wrong format!"

    )

    ;
  28. }
  29. if

    (

    my_check.

    checkMatchString

    (

    second_email)

    )
  30. {
  31. Console.

    WriteLine

    (

    "email "

    +

    second_email +

    " is in the right format!"

    )

    ;
  32. }
  33. else
  34. {
  35. Console.

    WriteLine

    (

    "email "

    +

    second_email +

    " is in the wrong format!"

    )

    ;
  36. }

  37. Console.

    ReadKey

    (

    )

    ;
  38. }

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.

Book traversal links for Basic Regular Expression in C#

  • ‹ Abstract Classes in C# Language
  • Up
  • Converting Data Types in C# Language ›

 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,513

356,536

356,563

Top
Raidforums