In: Computer Science
in c#
Regular Expressions. What are they? What do they do for us? What is their power and what challenges to they present to writing solid code?
Regular Expressions
A regular expression, regex or regexp (sometimes called a rational expression) is a sequence of characters that define a search pattern. Usually such patterns are used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.
Benefits of using Regular Expression
Regular expressions are used in search engines, search and replace dialogs of word processors and text editors, in text processing utilities such as sed and AWK and in lexical analysis. Many programming languages provide regex capabilities either built-in or via libraries.
For example: In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex.
C# provides a class termed as Regex which can be found in System.Text.RegularExpression namespace. This class will perform two things:
Below example demonstrate the use of regex in Mobile Number Verification. Suppose you are making a form where you need to verify the user-entered mobile number then you can use regex.
// C# program to validate the Mobile
// Number using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main(string[] args)
{
// Input strings to Match
// valid mobile number
string[] str = {"9925612824",
"8238783138", "02812451830"};
foreach(string s in str)
{
Console.WriteLine("{0} {1} a valid mobile number.", s,
isValidMobileNumber(s) ? "is" : "is not");
}
Console.ReadKey();
}
// method containing the regex
public static bool isValidMobileNumber(string inputMobileNumber)
{
string strRegex = @"(^[0-9]{10}$)|(^\+[0-9]{2}\s+[0-9]
{2}[0-9]{8}$)|(^[0-9]{3}-[0-9]{4}-[0-9]{4}$)";
// Class Regex Repesents an
// immutable regular expression.
// Format Pattern
// xxxxxxxxxx ^[0 - 9]{ 10}$
// +xx xx xxxxxxxx ^\+[0 - 9]{ 2}\s +[0 - 9]{ 2}\s +[0 - 9]{ 8}$
// xxx - xxxx - xxxx ^[0 - 9]{ 3} -[0 - 9]{ 4}-[0 - 9]{ 4}$
Regex re = new Regex(strRegex);
// The IsMatch method is used to validate
// a string or to ensure that a string
// conforms to a particular pattern.
if (re.IsMatch(inputMobileNumber))
return (true);
else
return (false);
}
}
Output:
9925612824 is a valid mobile number.
8238783138 is a valid mobile number.
02812451830 is not a valid mobile number.
There are many basic syntaxes like Quantifiers, Special Characters, Character Classes, Grouping & Alternatives are used for regular expressions.
Using regular expressions in any language seems to be challenging to provide the exact things what user says....