Six Simple Examples of C Regular Expressions

  • 2021-08-17 00:54:05
  • OfStack

In computer science, a regular expression is a single string that describes or matches a series of strings that conform to a syntactic rule. In many text editors or other tools, regular expressions are often used to retrieve and/or replace text content that conforms to a pattern. Many programming languages support string manipulation using regular expressions.

Let's look at regular expressions in C #.

Number 1: Verify that the input string is a number


/// <summary>  
///  Verify that the input string is a number   
/// </summary>  
/// <param name="P_str_num"> Input character </param>  
/// <returns> Return 1 A bool Value of type </returns>  
public bool validateNum(string P_str_num)  
{  
return Regex.IsMatch(P_str_num, "^[0-9]*$");  
} 

Number 2: Verify that the input string is a phone number


/// <summary>  
///  Verify that the input string is a phone number   
/// </summary>  
/// <param name="P_str_phone"> Input string </param>  
/// <returns> Return 1 A bool Value of type </returns>  
public bool validatePhone(string P_str_phone)  
{  
return Regex.IsMatch(P_str_phone, @"\d{3,4}-\d{7,8}");  
} 

Number 3: Verify that the input string is a fax number


/// <summary>  
///  Verify that the input string is a fax number   
/// </summary>  
/// <param name="P_str_fax"> Input string </param>  
/// <returns> Return 1 A bool Value of type </returns>  
public bool validateFax(string P_str_fax)  
{  
return Regex.IsMatch(P_str_fax, @"86-\d{2,3}-\d{7,8}");  
} 

Number 4: Verify that the input string is a zip code


/// <summary>  
///  Verify that the input string is a zip code   
/// </summary>  
/// <param name="P_str_postcode"> Input string </param>  
/// <returns> Return 1 A bool Value of type </returns>  
public bool validatePostCode(string P_str_postcode)  
{  
return Regex.IsMatch(P_str_postcode, @"\d{6}");  
} 

Number 5: Verify that the input string is an E-mail address


/// <summary>  
///  Verify that the input string is E-mail Address   
/// </summary>  
/// <param name="P_str_email"> Input string </param>  
/// <returns> Return 1 A bool Value of type </returns>  
public bool validateEmail(string P_str_email)  
{  
return Regex.IsMatch(P_str_email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");  
} 

Number 6: Verify that the input string is a network address


/// <summary>  
///  Verify that the input string is a network address   
/// </summary>  
/// <param name="P_str_naddress"> Input string </param>  
/// <returns> Return 1 A bool Value of type </returns>  
public bool validateNAddress(string P_str_naddress)  
{  
return Regex.IsMatch(P_str_naddress, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");  
}  

Now, this site collates these 6 points, and there are new ones to continue to be added in the future. I hope these six articles can be helpful to everyone's study.


Related articles: