Regular Expressions for C #

Source: Internet
Author: User

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Reflection;usingSystem.Text.RegularExpressions;namespacecodetest{classProgram {Static voidMain (string[] args) {            string[] Strarray = {"1111-222-333","1111-2222-3333" }; stringPattern =@"^\d{4}-\d{3}-\d{3}$"; foreach(varIteminchStrarray) {                if(Regex.IsMatch (item, pattern)) {Console.WriteLine ("{0} is vaild", item); }                Else{Console.WriteLine ("{0} is not vaild", item);            }} regexismacth ();            Regexreplace ();            Regexsplit ();            Regexmacths ();            Regexgroup ();        Console.ReadLine (); }        Static voidregexismacth () {stringinput ="This is my name my name"; stringPattern =@"\s\w{4}\s"; Match Match=Regex.match (Input,pattern);  while(match. Success) {Console.WriteLine (match. groups[0].                Value); Match=match.            NextMatch (); }        }        Static voidRegexreplace () {stringinput ="This is my name my name"; stringPattern =@"\s\w{4}\s"; stringReplacement ="Match";            Console.WriteLine (input);        Console.WriteLine (Regex.Replace (input, pattern, replacement)); }        Static voidRegexsplit () {stringinput ="1.A 2.B 3.C 4.D"; stringPattern =@"\s"; foreach(varIteminchRegex.Split (Input,pattern)) {                if(!string.                IsNullOrEmpty (item)) {Console.WriteLine (item); }            }        }        Static voidregexmacths () {regex regex=NewRegex (@"\d+");            MatchCollection matchs; Matchs= Regex. Matches ("123abc333adsa123123asdasd123123"); foreach(Match Iteminchmatchs) {Console.WriteLine ("value is {0},index is {1}", item. Value,item.                Index); Console.WriteLine ("Result is {0}", item. Result ("[$&]")); }        }        Static voidRegexgroup () {stringinput ="Born:july, 1989"; stringPattern =@"\b (\w+) \s (\d{1,2}), \s (\d{4}) \b"; Match Match=Regex.match (input, pattern);  for(inti =0; I < match. Groups.count; i++) {Console.WriteLine ("Group value is {0},index is {1}", match. Groups[i]. Value,match. Groups[i].            Index); }        }    }}

Recommended use of Microsoft's regular Expression tester tool test

Regular Expressions for C #

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.