C# regular expression is used for pattern matching in C# applications. This article explains the syntax of C# regular expressions with examples and how to create our own C# regular expression in a C# console application with examples of mobile number regular expressions. Also learn how to use Regex in C#.
Regular Expression in C#
C# Regex class is used for creating a C# regular expression. Regular expressions are a pattern matching standard for string parsing and replacement and is a way for a computer user to express how a computer program should look for a specified pattern in text and then what the program is to do when each pattern match is found.
Sometimes it is abbreviated "regex". They are a powerful way to find and replace strings that take a defined format.
C# Regex class syntax
The following basic syntax are used for regular expressions,
Quantifiers
You can group characters by putting them between square brackets.This way, any character in the class will match one character in the input.
- [ ] => It is used to match a range of characters.
Example
![range of characters]()
Grouping and alternatives
It's often necessary to group things together with parentheses ( and ).
- ()=> It is used to group expressions.
Example
![different expression]()
In the preceding image the | operator is the Or operator that takes any of the alternatives.
- {} =>It is used to match the preceding character for a specified number of times.
i) {n}=> Matches the previous element exactly n times.
Example
![Grouping and alternatives]()
ii) {n,m} =>Matches the previous element at least n times, but no more than m times.
Example
![element at least n times]()
Example - Regex for Mobile Number Validation
Now in the following procedure, I will explain how to create our own regular expression for Mobile Number validation in a C# console application.
Step-by-step creation of a Regular Expression
Step 1
Open Visual Studio 2013.
Step 2
Then click on "File" > "New" > "Project..." ( or press "Ctrl +Shift + N").
Step 3
Then select Console Application, provide the name of the application "RegularExpression1" and click on OK.
Step 4
After adding the project "RegularExpression1" you will see the following code in the "Program.cs" file and for creation of the regex add the namespace:
using System.Text.RegularExpressions; Step 5
Now write the following code in the program.cs file to create the regex.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
-
- namespace RegularExpression1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Regex r = new Regex(@"^\+?\d{0,2}\-?\d{4,5}\-?\d{5,6}");
-
-
- string[] str = { "+91-9678967101", "9678967101", "+91-9678-967101", "+91-96789-67101", "+919678967101"};
-
-
- foreach(string s in str)
- {
- Console.WriteLine("{0} {1} a valid mobile number.", s,
- r.IsMatch(s) ? "is":"is not");
-
-
- }
- }
- }
- }
Step 6
After writing the code build program then run it and get the following output,
Explanation of Regular Expression Pattern
It is one way for creation of a Mobile Number validation RegularExpression.
It is all about how to create a RegularExpression in C#.