C # Regular Expression Regex common match,
To use the Regex class, you must reference the namespace: using System. Text. RegularExpressions;
Verification using the Regex class
Example 1: The annotated code plays the same role, but one is a static method and the other is an instance method
var source = "Liu Beiguan Yu Zhang Fei Sun Quan asked why";
// Regex regex = new Regex ("孙权");
// if (regex.IsMatch (source))
// {
// Console.WriteLine ("The string contains sensitive words: Sun Quan!");
//}
if (Regex.IsMatch (source, "Sun Quan"))
{
Console.WriteLine ("The string contains sensitive words: Sun Quan!");
}
Console.ReadLine ();
Example 2: Use a constructor with two parameters, the second parameter indicates to ignore the case, very common
var source = "123abc345DEf";
Regex regex = new Regex ("def", RegexOptions.IgnoreCase);
if (regex.IsMatch (source))
{
Console.WriteLine ("The string contains sensitive words: def!");
}
Console.ReadLine ();
Replace with Regex class
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 ();
result:
Original string: 123abc456ABC789
Replaced string: 123 | 456 | 789
Example 2: Replace the matched options with 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 ();
// Roucheng
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 commonly used matching
Online test: http://tool.hovertree.com/a/zz/
1 #region ID card number regular expression
2 // He asked
3
4 Console.WriteLine ("Please enter an ID number");
5 string id = Console.ReadLine ();
6 bool b4 = Regex.IsMatch (id, @ "^ \ d {15} | \ d {18} $");
7 bool b5 = Regex.IsMatch (id, @ "^ (\ d {15} | \ d {18}) $");
8 Console.WriteLine (b4);
9 Console.WriteLine (b5);
10
11 #endregion
12
13 #region Match phone number
14 // hovertree
15
16
17 Console.WriteLine ("Please enter the phone number");
18 string phone = Console.ReadLine ();
19 bool b = Regex.IsMatch (phone, @ "^ ((\ d {3,4} \-\ d? {7,8}) | (\ d {5})))"
20 Console.WriteLine (b);
twenty one
22 #endregion
twenty three
24 #region match email regex
25
26 // hovertree
27
28 Console.WriteLine ("Please enter Email address");
29 string email = Console.ReadLine ();
30 bool bhvt = Regex.IsMatch (email, @ "^ \ w + @ \ w + \. \ W + $");
31 Console.WriteLine (bhvt);
32
33 #endregion
34
35 #region regex matching ip address
36 // hovertree
37
38 Console.WriteLine ("Please enter an IP address");
39 string ip = Console.ReadLine ();
40 bool bkly = Regex.IsMatch (ip, @ "^ \ d {1,3} (\. \ D {1,3}) {3} $");
41 Console.WriteLine (bkly);
42
43 #endregion
44
45 #region match date legal regex
46 // He asked
47
48 Console.WriteLine ("Please enter a date");
49 string date = Console.ReadLine ();
50 bool bhovertree = Regex.IsMatch (date, @ "^ \ d {4} \-\ d {1,2} \-\ d {1,2} $");
51 Console.WriteLine (bhovertree);
52
53 #endregion
54
55
56 #region Regex matching url address
57 //"http://hovertree.com "
58 //"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa "
59 //"https://s.taobao.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8 "
60 //"ftp://127.0.0.1/myslider.txt "
61
62 // hovertree
63
64 Console.WriteLine ("Please enter url address");
65 string url = Console.ReadLine ();
66 bool bkeleyi = Regex.IsMatch (url, @ "^ [a-zA-Z] +: //.+$");
67 Console.WriteLine (bkeleyi);
68
69 #endregion
ASP.NET open source CMS http://www.cnblogs.com/sosoft/p/cms.html
Collection of development technical articles http://www.cnblogs.com/sosoft/p/kaifajishu.html