Regular Expression in C# Tutorial | Everything You Need to Know
Regular Expression in C Tutorial ACTE

Regular Expression in C# Tutorial | Everything You Need to Know

Last updated on 01st Feb 2022, Blog, Tutorials

About author

Jagan Mohan (Senior C# Developer )

Jagan Mohan is a Senior Senior C# Developer and has extensive knowledge in the following areas: WPF, XML, REST, Agile, V Model, C#, SQL Server, Web API, ASP, and.NET.

(5.0) | 19685 Ratings 2613
    • Introduction to Regular Expression in C#
    • Configuration for defining regular expressions
    • Regex magnificence
    • Description
    • C# regular expression class method
    • Inline options can be specified in two ways
    • RegexOptions enumerationlection in C#
    • Conclusion

    Subscribe For Free Demo

    [custom_views_post_title]

      Introduction to Regular Expression in C#

      Regular expressions are patterns that can be matched against input text. The .Net Framework provides a regular expression engine that enables such matching. A pattern consists of one or more character literals, operators, or components.


      Configuration for defining regular expressions

      There are several categories of characters, operators, and configurations that you can use to define regular expressions. Click the links below to find these configurations.


    • Escape character
    • Character class
    • Anchor
    • Grouping syntax
    • Quantumizer
    • Reverse reference syntax
    • Alternative syntax
    • Substitution

    • Other syntax
    • Regular expressions (additionally acknowledged as “normal expressions”) are a effective manner to locate matching styles in textual content and optionally update matching textual content. These provide a extra flexible technique than literal searches and textual content replacements.


      I were the usage of C# normal expressions professionally for over a decade to transform textual content from one layout to any other and extract records from textual content files. Regular expressions may be processed with a unmarried line of code that calls for more than one strains of code in C#.


      This article describes the maximum beneficial functions of the normal expression C# magnificence. And I anticipate you’ve got got at the least a primary knowledge of regex sample syntax and C# syntax.


      Regex magnificence

      The Regex C# magnificence presents techniques to healthy textual content with a regex sample. There also are techniques that take delivery of C# code to update the matched textual content, letting you leverage regex and C# common sense to carry out locate and update operations.


      The magnificence is in the .NET library System.Text.RegularExpressions. This library ships with all variations of .NET, so in case you have .NET set up in your machine, you may effortlessly use this library to your C# project.


      Also, the Regex magnificence has static and example variations of maximum techniques. This article will consciousness at the static model. I especially use the static model of the method. Because they’re extra convenient. (In the example model, you want to create a normal expression item earlier than calling it.)


      Regular expression class

      Regular expression class is used to represent a regular expression.

      The commonly used methods are:

      1. Senior Number Method and Description

      public bool IsMatch (String input) Regex Indicates whether the regular expression specified in constructor finds a match in the specified input string.


      2. public bool IsMatch (string input, int)

      The REGEX work matches a string to a normal articulation and returns valid (1) assuming it matches and bogus (0) in the event that it doesn’t coordinate.


      3. public static bool IsMatch (string input, string pattern)

      Indicates whether the specified regular expression finds a match in the specified input string.


      4. public MatchCollection Matches

      Matches all occurrences of the regular expression in the specified input string


      5. public string Replace

      All strings that match the regular expression pattern are specified. Replaces with the specified replacement string in the input string.


      6. public string [] Split (string input)

      Splits the input string into an array of substrings at the positions defined by the regular expression pattern specified by the Regex constructor.


      C# regular expression example

      In the previous section, you learned about regular expression symbols. This section details how different symbols are used in regular expressions and the combinations that can be used to match different expressions. This tutorial describes some of the most common real-world scenarios that developers may encounter when capturing user input in an application or simple program.

      Course Curriculum

      Learn Advanced C Programming Certification Training Course to Build Your Skills

      Weekday / Weekend BatchesSee Batch Details
    • Regular Expression Examples Using Real World Scenarios
    • Learn more about regular expressions with some real-time examples.

    • Scenario 1: Check if the input string consists of 6 uppercase and lowercase letters.

      The most common regular expression scenario is to find and match a particular word. Suppose you need a random alphabet string from the user and the input must be exactly 6 digits long. Check if you can use a simple regular expression. Let’s write a program to better understand how to write and use regular expressions.


      • public static void Main (string [] args)
      • {
      • string patternText = @ “^ [azAZ] {6} $”;
      • Regular expression reg = new Regex (SampleText);
      • // Pattern is
      • Console.WriteLine (reg) .IsMatch (“Helios”)); If it matches
      • // If the pattern does not match
      • Console.WriteLine (reg.IsMatch (“Helo”));

      • }

      Output

    • True
    • False

    • Description

    • In this example, input I’m trying to validate the string to see if it contains 6 digits. Characters can be both lowercase and uppercase, so that must be taken into account as well.

    • Now I have defined a regular expression pattern in the patternText variable and passed it to the Regex object. The following line of code is very simple. I used the IsMatch method to compare the regular expression with the input string. Let’s take a look at the regular expressions that came up in . [DATA EXPUNGED] ^ [azAZ] {6} $) consists of four different parts. “^”, “[AzAZ]”, “{6}”, “$”. The second part identifies the matching characters used to perform the expression match. “Az” for lowercase letters and “AZ” for uppercase letters.

    • The first subcharacter “^” guarantees that the string starts with the pattern defined in the second part. H. Lowercase and uppercase. The curly braces in the third part determine the number of characters in the string that can be identified by the defined pattern (6 in this case), and the “$” symbol matches the pattern defined in the second part. Guarantee. ^[azAZ]{6}$

    • Regular Expression in C#

      In C#, Regular Expression is used for the parsing and validation of the given textual content to in shape with the described sample (for instance, an e mail address). The sample can comprise operators, characters literals, or constructs.


      To system the textual content with the normal Expression in .NET Framework, generally, we used the normal expression engine. In C#, Regular Expression is proven via way of means of the Regex.


      Regular Expression

    • We used the Regular Expression to test whether or not the given string fits the sample or not. A normal expression or normal expression is a string that defines a sample. Patterns can encompass numbers, literals, operators, characters, and more. I looked for a string or document the use of a sample. Now take a look at to see if a in shape become found.

    • Regular expressions are typically used for parsing, string retrieval, validation, and so on. Here we’re taking an instance wherein we are able to use Regular Expression for checking the social safety number, legitimate date of birth, for matching the total call wherein the primary and closing call are separated via way of means of the comma, for changing the substring, for the perfect e mail layout, forex layout and so on.

    • Regex Class

    • Regex magnificence suggests the normal expression engine in the .NET Framework. Regex magnificence is used to parse a massive quantity of textual content for locating the precise man or woman sample. We can use the Regex magnificence for the extraction, editing, for the replacement, or to delete the textual content of the substring.

    • System.Text.RegularExpressions namespace incorporates the Regex magnificence. The Regex magnificence has a string sample as a parameter with different optionally available parameters.

    • Next, let`s create a sample from a normal expression. This code calls for the sample to in shape a phrase that starts with the letter “M”.

    • C# regular expression class method

      The Regex class contains different methods to perform different operations on the input string. This table contains a list of various C# regular expression methods.

      Method description

      IsMatch We used the IsMatch method to check if the specified input string matches the regular expression pattern. The Matches match method is used to return text that matches the regular expression pattern. The Replace-Replace method is used to replace text that matches the regular expression pattern. I used the Split Split method to split the string into an array of substrings at positions that match the regular expression pattern. The methods of the Regex class above are used to validate, replace, or split string values ​​in a regular expression pattern based on your requirements.


      Regular expression options

      You can specify options that control how the regular expression engine interprets regular expression patterns. Many of these options can be specified as inline (regular expression pattern) or as one or more RegexOptions constants. This quick reference only lists inline options. For more information on inline and RegexOptions options, see the Regular Expression Options article.


      Inline options can be specified in two ways:

      Use another configuration (? Imnsximnsx). Here, the minus sign () in front of an option or set of options turns off these options. Example: (? Imn) turns on case-insensitive comparison (i), multi-line mode (m), and unnamed grouping (n) detection.


      Course Curriculum

      Get JOB Oriented C Programming Training for Beginners By MNC Experts

      • Instructor-led Sessions
      • Real-life Case Studies
      • Assignments
      Explore Curriculum

      The option applies to the regular expression pattern from the point where the option is defined, and to either the end of the pattern or the point where the option is inverted by another configuration. Use a grouping structure (? Imnsximnsx: subexpression) that defines options only for the specified group.


      Regex Replace String Example in C# With the assist of this example, we’re attempting to find the substring via way of means of the use of the everyday expression sample, which replaces the specified values in C#.


      • the use of System;
      • the use of System.Text.RegularExpressions;
      • namespace ConsoleApp1
      • magnificence Program
      • “, result);
      • Console.ReadLine();
      • }
      • }
      • }

      In the above example, we used the Regex. Replace approach is used for locating and alternative of the unique characters in a string with the gap via way of means of the use of the everyday expression sample (“[^a-zA-Z0-9_]+”).

      In the above example, the sample of the Regular [removed]”[^a-zA-Z0-9_]+”) attempted to in shape with the unmarried character, which isn’t always described withinside the organization of the character.


      Find the Duplicate Words in Regex C#

      By using the regular expression pattern, we can easily find out the duplicate words. In this example, we are trying to find the duplicate word in the given string by using the Regex class method in C#.


      • using System;
      • using System.Text.RegularExpressions;
      • namespace ConsoleApp1
      • {
      • class Program1
      • {
      • static void Main(string[] args)
      • {
      • string str1 = “Welcome To to JavaTPoint. Here we can learn C# easily easily way”;
      • MatchCollection collection = Regex.Matches(str1, @”\b(\w+?)\s\1\b”, RegexOptions.IgnoreCase);
      • foreach (Match m in collection)
      • {
      • Console.WriteLine(“{0} (duplicates `{1}’) at position {2}”, m.Value, m.Groups[1].Value, m.Index);
      • }
      • Console.ReadLine();
      • }
      • }
      • }

      The above example used the Regex. Matches method is used to find out the duplicate words by using the regular expression pattern (“\b(\w+?)\s\1\b”).


      IsMatch () method

      Let’s start with the most basic method, IsMatch (). This method tests if the regular expression pattern string matches anywhere in the specified input string and returns true or false. Think of this method as a more advanced version of string.Contains ().

      This is a simple example of testing whether a text variable contains lowercase letters.

      • var text = “test”;
      • var hasLetter = Regex.IsMatch (Text, “[az]”);

      In this example, the regular expression pattern [az] matches the first lowercase letter t in the text, so a Boolean variable hasLetter is set to true.


      Match () method

      The Match () method is built on IsMatch () by returning a Match object that contains information about the first text that the regular expression pattern matches. The Match object contains information such as the actual matched text segment (Value property) and the starting index (Index property) of the matched text segment in the input text. This method is useful for extracting text from a string. The following is an example of getting the first lowercase letter from a string.


      • vartext = “text example”;
      • var match = Regex.Match (text, “[az]”);
      • var firstLowercaseLetter = match.Value;

      In this example, the Match () method is a regular expression pattern [az]. , is used to match the first lowercase letter of the text. The Match () method returns a Match object and the firstLowercaseLetter is assigned a matching text value via the Value property.

      This method finds only the first occurrence that matches the pattern. If you expect the regular expression pattern to contain multiple matches in a string, you can use the Matches () method to get a MatchCollection (a collection of Match objects). Here is an example:


      • vartext = “example text”;
      • variable match = Regex.Matches (text, “[az]”);
      • foreach
      • {
      • var letter = match.Value;
      • // do something with a string
      • }

      In this example, the Matches () method finds all lowercase letters that appear in the text and returns a MatchCollection that represents a match. The variable “Matches” is then assigned to the MatchCollection and each match is used by iterating over the matches in a foreach loop.


      Replace() method

      The Replace() method takes functionality to the next level. You can use this method to match text like with the Match() method above, but then you can use a replacement string to replace the matched text. The method returns a copy of the input text modified with the replaced text in place of the matched text segments.


      The static version of this method replaces all match occurrences; the instance version allows you to set a limit to how many occurrences are replaced. In my experience, I have almost always wanted to replace all occurrences. Below is an example of how to find a number enclosed in bold HTML tags and replace it with an italic tag.


      • var text = “1. First period \ n2. Second period”;
      • text = Regex.Replace (text, @ “(\ d +) \.”, “$ 1.”);

      In this example, The Replace () method searches the text to see if all bold numbers and period tags appear. Then replace each occurrence with an italic tag, leaving the number and period defined in the replacement string. The variable text is updated as follows:


    • “First point \ n2.
    • Note that the dot ” match pattern uses a capture group (\ d +) to store a number in the variable pointed to by the replacement string via $
    • Regular expression brackets are referenced.
    • Used to enclose possible capture group specifications.
    • The first capture group in the pattern is referenced by variable $ 1 and the second capture group is referenced by the replacement string in variable $ just before the start quote in the string.
    • The literal C# string eliminates the need for a C# escape (implemented by another backslash) for each backslash character (\), shortening the regular expression pattern and making it easier to read.

    • RegexOptions enumeration

      All of the above methods (except the Escape () method) have an overload that accepts the RegexOptions enum parameter that you can use to set various regular expression options. These are the options I find most valuable:

      IgnoreCase: This option makes pattern matching case insensitive. (By default, pattern matching is case sensitive.)

      SingleLine: This option changes the dot meta character (.) In the pattern string to match any character, including newline characters. This is useful when pattern matching needs to span multiple lines of input text.

      Compiled: This option compiles the regular expression into an assembly, which makes the regular expression run faster. I used it sparingly. However, this option can be used if the input text you are looking for is very large, or if each match can span multiple lines of input text. If you think you should use this option, you must first make sure that the regular expression pattern is spelled correctly. Alternatively, you may need to split the input text into smaller pieces so that the regular expression can be executed on the smaller text.


      Escape () method

      The Escape () method escapes the metacharacters in the string so that they are treated as literal characters in the regular expression pattern. Examples of metacharacters are period (.) And asterisk (*) characters. This is especially useful for dynamically assembling regular expression patterns at run time, as opposed to certain string patterns as in all previous code examples. When you dynamically create a regular expression pattern, you can include the metacharacters you want to match as literals. Escaping metacharacters in text prevents you from creating invalid regular expression patterns or regular expression patterns that don`t match what you expect.


      C and C Plus Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

      Conclusion

      End of String or Line: $ The $ anchor determines that the previous example should happen toward the finish of the information string, or before \n toward the finish of the info string. Assuming you use $ with the RegexOptions. Multiline choice, the match can likewise happen toward the finish of a line. The string should end with a section and just numbers inside it. The end section ought to show up just a single time as far as it goes and not elsewhere. Any characters are permitted before the section begins. No characters are permitted after the section end.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free