C # Regular Expression Regex common match,

Source: Internet
Author: User
Tags open source cms

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 Bei, Guan Yu, Zhang Fei, Sun Quan, and He asked";
// Regex regex = new Regex ("Sun Quan");
// 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 case-insensitive and is 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 the Regex class

Example 1: simple

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 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 ();

  // 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 common matching

Online test: http://tool.hovertree.com/a/zz/

1 #region ID 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 a 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 Regex matching email
25
26 // hovertree
27
28 Console.WriteLine ("Please enter your 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 the 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

Development Technology Article collection http://www.cnblogs.com/sosoft/p/kaifajishu.html


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.