Question

In: Computer Science

Regular Expressions. What are they? What do they do for us?

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?

Solutions

Expert Solution

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

  • Wide range of usage possibility, you may create one regular expression to validate any kind of input;
  • Supported by almost any language, there are only a few programming languages which do not understand regular expressions;
  • Do more with less, keep your code cleaner;
  • Faster validations, instead of having many IF and ELSE operators you may validate only once with a 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:

  • Parsing the inputing text for the regular expression pattern.
  • Identify the regular expression pattern in the given text.

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....


Related Solutions

RegEx (Regular Expressions) Make 8 regular expressions to filter a specific data set in Java and...
RegEx (Regular Expressions) Make 8 regular expressions to filter a specific data set in Java and explain what they do.
Linux regular expressions: file name: lab3test.txt Create regular expressions that meet the following criteria. You may...
Linux regular expressions: file name: lab3test.txt Create regular expressions that meet the following criteria. You may use grep or egrep to answer these questions. 1.Match any lines that contain a phone number with the format 222-222-2222.
linux Regular expressions file name: lab3test.txt Create regular expressions that meet the following criteria. You may...
linux Regular expressions file name: lab3test.txt Create regular expressions that meet the following criteria. You may use grep or egrep to answer these questions. 1.Display all lines where the employee’s age is greater than 40 years old. Again, don’t think of this as a number, think of it as a series of characters.
Regular => Context-Free Give a proof by induction on regular expressions that: For any language A,...
Regular => Context-Free Give a proof by induction on regular expressions that: For any language A, if A is regular, then A is also context-free. You may assume that the statements from the previous Closure under Context-Free Languages problem are proved. R is a regular expression if RR is: a for some a∈alphabet Σ, the empty string ε, the empty set ∅, R​1​​∪R​2​​, sometimes written R​1​​∣R​2​​, where R​1​​ and R​2​​ are regular expressions, R​1​​∘R​2​​, sometimes written R​1​​R​2​​, where R​1​​ and...
5 Regular => Context-Free Give a proof by induction on regular expressions that: For any language...
5 Regular => Context-Free Give a proof by induction on regular expressions that: For any language A, if A is regular, then A is also context-free. You may assume that the statements from the previous Closure under Context-Free Languages problem are proved.
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided 1.Show only the lines that end with an ellipses (3 dots) :
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
Regular Expressions Assignment Write a regular expression for each of the following. Can you show output...
Regular Expressions Assignment Write a regular expression for each of the following. Can you show output please. A blank line (may contain spaces) Postal abbreviation (2 letters) for State followed by a space and then the 5-digit zip code A KU student username (Ex. lpork247) A “valid” email address (also explain how you defined “valid”) A SSN pattern (ddd-dd-dddd)
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
Write a regular expressions that would match lines in text that contain word DATE: at the...
Write a regular expressions that would match lines in text that contain word DATE: at the beginning of a line followed by actual date in format YYYY-MM-DD. Space between colon ( : ) and date may or may not exist. In C, if you issue the following statement n << 2 where n is an integer, what will be value of n? In bash, if you define variable var = “date” and issue a statement echo `$var`, what output will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT