C # Regular Expression Regex common match,

Source: Internet
Author: User

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 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 (" 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 (); // 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 matching

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

1 # region ID card number Regular Expression 2 // Why? 3 4 Console. writeLine ("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 # endregion12 13 # region Matching phone number 14 // hovertree15 16 17 Console. writeLine ("Enter the phone number"); 18 string phone = Console. readLine (); 19 bool B = Regex. isMat Ch (phone, @ "^ (\ d {3, 4} \-\ d? {7, 8}) | (\ d {5}) $ "); 20 Console. writeLine (B); 21 22 # endregion23 24 # region matches the email's regex25 26 // hovertree27 28 Console. writeLine ("enter Email address"); 29 string email = Console. readLine (); 30 bool bhvt = Regex. isMatch (email, @ "^ \ w + @ \ w + \. \ w + $ "); 31 Console. writeLine (bhvt); 32 33 # endregion34 35 # region matching IP address regex36 // hovertree37 38 Console. writeLine ("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 # endregion44 45 # region Matching date valid regex46 // Why do you ask 47 48 Console. writeLine ("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 # endregion54 55 56 # region Matching url address regex57 // "http://hovertree.com" 58 // "http://keleyi.com/a/bja E/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 // hovertree63 64 Console. writeLine ("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

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.