Validate Input Using Regular Expressions - CSharp Language Basics

CSharp examples for Language Basics:Regex

Introduction

Commonly Used Regular Expression Metacharacter Elements

Element Description
. match any character except a newline character (\n)
\d match any decimal digit
\D match any nondigit
\s match any whitespace character
\S match any non-whitespace character
\w match any word character
\W match any nonword character
^ match the beginning of the string or line
\Amatch the beginning of the string
$ match the end of the string or line
\zmatch the end of the string
| Matches one of the expressions separated by the vertical bar (pipe symbol); for example, AAA|ABA|ABB will match one of AAA, ABA, or ABB (the expression is evaluated left to right)
[abc] match with one of the specified characters; for example, [AbC] will match A, b, or C, but no other characters
[^abc]match with any one character except those specified; for example, [^AbC] will not match A, b, or C, but will match B, F, and so on
[a-z] match with any one character in the specified range; for example, [A-C] will match A, B, or C
( ) Identifies a subexpression so that it?s treated as a single element by the regular expression elements described in this table
? match one or zero occurrences of the previous character or subexpression; for example, A?B matches B and AB, but not AAB
* match zero or more occurrences of the previous character or subexpression; for example, A*B matches B, AB, AAB, AAAB, and so on
+ match one or more occurrences of the previous character or subexpression; for example, A+B matches AB, AAB, AAAB, and so on, but not B
{n} match exactly n occurrences of the preceding character or subexpression; for example, A{2} matches only AA
{n,} match a minimum of n occurrences of the preceding character or subexpression; for example, A{2,} matches AA, AAA, AAAA, and so on, but not A
{n, m}match a minimum of n and a maximum of m occurrences of the preceding character; for example, A{2,4} matches AA, AAA, and AAAA, but not A or AAAAA

Commonly Used Regular Expressions

Input Type
Numeric input

Description
The input consists of one or more decimal digits; for
example, 5 or 1234567874.
Regular Expression
^\d+$

Personal
identification
number (PIN)
The input consists of four decimal digits; for example,
1234.

^\d{4}$


Simple password

The input consists of six to eight characters; for example,
pas123 or bew87h.
^\w{6,8}$

Credit card
number

The input consists of data that matches the pattern of
most major credit card numbers; for example,
4111111111111111 or 4111-1111-1111-1111.
^\d{4}-?\d{4}-
?\d{4}-?\d{4}$

E-mail address



The input consists of an Internet e-mail address. The [\w-
]+ expression indicates that each address element must
consist of one or more word characters or hyphens; for
example, somebody@adatum.com.
^[\w-]+@([\w-
]+\.)+[\w-]+$


HTTP or HTTPS
URL

The input consists of an HTTP-based or HTTPS-based
URL; for example, http://www.book2s.com.

^https?://([\w-
]+\.)+ [\w-]+(/[\w-
./?%=]*)?$

Demo Code

using System;//w w w. j  av a  2 s  .  c o  m
using System.Text.RegularExpressions;
class MainClass
{
   public static bool ValidateInput(string regex, string input)
   {
      Regex r = new Regex(regex);
      return r.IsMatch(input);
   }
   public static void Main(string[] args)
   {
      Console.WriteLine("Regular Expression: {0}", args[0]);
      Console.WriteLine("Input: {0}", args[1]);
      Console.WriteLine("Valid = {0}", ValidateInput(args[0], args[1]));
   }
}

Result


Related Tutorials