Common Matching of C Regular Expression Class Regex
- 2021-08-31 08:52:44
- OfStack
Using the Regex class requires a reference to the namespace: using System. Text. RegularExpressions;
Using Regex class to realize verification
Example 1: The annotated code serves the same purpose, except that one is a static method and one is an instance method
var source = " What did Liu Bei, Guan Yu, Zhang Fei and Sun Quan ask ";
//Regex regex = new Regex(" Sun Quan ");
//if (regex.IsMatch(source))
//{
// Console.WriteLine(" String contains sensitive words : Sun Quan! ");
//}
if (Regex.IsMatch(source, " Sun Quan "))
{
Console.WriteLine(" String contains sensitive words : Sun Quan! ");
}
Console.ReadLine();
Example 2: Using a constructor with two parameters, the second parameter indicates ignoring case, which is very common
var source = "123abc345DEf";
Regex regex = new Regex("def",RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
{
Console.WriteLine(" String contains sensitive words :def ! ");
}
Console.ReadLine();
Use the Regex class for replacement
Example 1: Simple case
var source = "123abc456ABC789";
// Static method
//var newSource=Regex.Replace(source,"abc","|",RegexOptions.IgnoreCase);
// Instance method
Regex regex = new Regex("abc", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source, "|");
Console.WriteLine(" Original string: "+source);
Console.WriteLine(" Replaced string: " + newSource);
Console.ReadLine();
Results:
Original String: 123abc456ABC789
Replaced string: 123456789
Example 2: Replace the matching option with the html code, we use the MatchEvaluator delegate
var source = "123abc456ABCD789";
Regex regex = new Regex("[A-Z]{3}", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source,new MatchEvaluator(OutPutMatch));
Console.WriteLine(" Original string: "+source);
Console.WriteLine(" Replaced string: " + newSource);
Console.ReadLine();
// Soft City
private static string OutPutMatch(Match match)
{
return "<b>" +match.Value+ "</b>";
}
Output:
Original String: 123abc456ABCD789
Replaced string: 123 < b > abc < /b > 456 < b > ABC < /b > D789
C # Regular Expression Regex Common Matches
Online test: http://tool.hovertree.com/a/zz/
#region ID card number regular expression
// He asked
Console.WriteLine(" Please enter 1 ID number ");
string id = Console.ReadLine();
bool b4 = Regex.IsMatch(id, @"^\d{15}|\d{18}$");
bool b5 = Regex.IsMatch(id, @"^(\d{15}|\d{18})$");
Console.WriteLine(b4);
Console.WriteLine(b5);
#endregion
#region Match phone number
//hovertree
Console.WriteLine(" Please enter a phone number ");
string phone = Console.ReadLine();
bool b = Regex.IsMatch(phone, @"^((\d{3,4}\-\d?{7,8})|(\d{5}))$");
Console.WriteLine(b);
#endregion
#region Matching email Adj. regex
//hovertree
Console.WriteLine(" Please enter Email Address ");
string email = Console.ReadLine();
bool bhvt = Regex.IsMatch(email, @"^\w+@\w+\.\w+$");
Console.WriteLine(bhvt);
#endregion
#region Matching ip Address regex
//hovertree
Console.WriteLine(" Please enter 1 A IP Address ");
string ip = Console.ReadLine();
bool bkly = Regex.IsMatch(ip, @"^\d{1,3}(\.\d{1,3}){3}$");
Console.WriteLine(bkly);
#endregion
#region Matching date is legal regex
// He asked
Console.WriteLine(" Please enter 1 Date ");
string date = Console.ReadLine();
bool bhovertree = Regex.IsMatch(date, @"^\d{4}\-\d{1,2}\-\d{1,2}$");
Console.WriteLine(bhovertree);
#endregion
#region Matching url Address regex
//"http://hovertree.com"
//"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa"
//"https://s.taobao.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8"
//"ftp://127.0.0.1/myslider.txt"
Console.WriteLine(" Please enter url Address ");
string url = Console.ReadLine();
bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$");
Console.WriteLine(bkeleyi);
#endregion