Easily learn the regular expression of C

  • 2021-08-21 21:14:35
  • OfStack

When writing programs that deal with strings, it is often necessary to find strings that conform to some complex rules. Regular expressions are the tools used to describe these rules. Regular expressions have their own grammar rules. Common grammar includes character matching, repetition matching, character positioning, escape matching and other advanced grammar (character grouping, character replacement and character decision). When using regular expressions, regular expressions are constructed first, which uses Regex class. It is constructed in two ways:
Basic form Regex (regular expression)
Basic form Regex (regular expression, match option)
The matching option is to provide 1 special help and is an enumerated value, including the following 6 values:

(1) IgnoreCase (case ignored) (2) ReghtToLeft (left to right) (3) None (default) (4) CultureInvariant (Ignore Region) (5) Multline (multiline mode) (6) SingleLine (single line mode)

1. Matching regular expressions
The matching of regular expressions is achieved through the IsMatch method of the Regex class, and the IsMatch method is used in the following format:
Regex. IsMatch (string to judge, regular expression)
Example 1: Using IsMatch method to judge whether it conforms to the local telephone number


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions;// Introducing namespaces  
using System.Threading.Tasks; 
 
namespace  Regular expression  
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 string str = @"(0530|0530-)\d{7,8}";// The defined regular expression meets the conditions of " 0530 "Or  
 Console.WriteLine(" Please enter 1 Telephone number ");// " 0530- "At the beginning, followed by 7 Bit or 8 Bit number  
 string tel = Console.ReadLine(); 
 bool b; 
 b = Regex.IsMatch(tel,str);// Determine whether it matches the regular expression  
 if (b) 
 { 
 Console.WriteLine("{0} Is the telephone number of a certain place ",tel); 
 } 
 else 
 { 
 Console.WriteLine("{0} It's not a telephone number of a certain place ",tel); 
 } 
 Console.ReadLine(); 
 } 
 } 
}</span> 

Input: 0530-12345678
The output result is: 0530-12345678 is the telephone number of a certain place
2. Replacement of regular expressions
To replace a match in a string with a regular expression, use the Replace method in the Regex class. One commonly used format is:
Regex. Replace (string to search for matches, original string to replace, replaced string)
Example 2: A program that uses the Replace method to replace "@" with "AT" in the e-mail entered by the user


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
 
namespace ConsoleApplication2 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 string str = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";// Regular expression for the defined e-mail address  
 Console.WriteLine(" Please enter 1 A correct one Internet E-mail address "); 
 string email = Console.ReadLine(); 
 bool b; 
 b = Regex.IsMatch(email, str);// Determine whether it matches the regular expression  
 if (b) 
 { 
 string outstr = ""; 
 outstr = Regex.Replace(email, "@", "AT");// Make a replacement  
 Console.WriteLine(" After replacement: {0}", outstr); 
 } 
 else 
 { 
 Console.WriteLine(" The string you entered does not include Internet URL"); 
 } 
 Console.ReadLine(); 
 } 
 } 
}</span> 

Input: 123456 @ 126.com
The output result is: 123456AT126. com
3. Splitting regular expressions
To split a string through a regular expression, use the Split method of the Regex class in the format:
Regex. Split (string to split, regular expression pattern to match)
Example 3: Splitting the input string through Split method


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
 
namespace ConsoleApplication2 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 string str = ";";// Defined regular expression  
 Console.WriteLine(" Please enter multiple user names separated by semicolons "); 
 string names = Console.ReadLine(); 
 string[] name; 
 name = Regex.Split(names,str); 
 Console.WriteLine(" The separated name is: "); 
 foreach (string item in name) 
 { 
 Console.WriteLine(item); 
 } 
 Console.ReadLine(); 
 } 
 } 
}</span> 

Input: Zhang 3; Li 4; Wang 5
The output result is: Zhang 3
Lee 4
Wang 5

The above is the regular expression of C #, hoping to help everyone learn.


Related articles: