C # Learning record 5--regular expressions Regular expression

Source: Internet
Author: User

A regular expression that uses a string to match a series of strings that conform to a certain standard. Very useful for checking the format of strings, for example, checking the format of information when registering for a login.

C # Regular expression under namespace System.Text.RegularExpressions

Example 1.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Text.RegularExpressions;7 namespaceregextest8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             varSTR1 ="One , three four."; -Regex reg =NewRegex (",|;| "); the             foreach(stringStrinchReg. Split (str1)) -             { -                 if(str. Length! =0) - Console.WriteLine (str); +                 } - Console.readkey (); +         } A     } at}

Line six, using the corresponding namespace
14 lines, create an instance of the regex, the contents of the parentheses indicate: a single comma or a single semicolon or a single space

15 lines in foreach, Reg. Split (str1) means that str1 is split as regular expressions by calling the split method of the Reg instance

Get results

Example 2

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Text.RegularExpressions;7 namespaceregextest8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             varSTR1 ="text, Test tux tax."; -Regex reg =NewRegex (@"(t\s+t) (\s|,\s|;\ s|.)"); theMatchCollection coll1 =Reg. Matches (STR1); -             foreach(Match matchinchcoll1) -             { -                 if(Match. Length! =0) + Console.WriteLine (match. ToString ()); -             } + Console.WriteLine (); ARegex REG2 =NewRegex (@"(t\s+x) (\s|,\s|;\ s|.)"); atMatchCollection coll2 =REG2. Matches (STR1); -             foreach(Match matchinchcoll2) -             { -                 if(Match. Length! =0) - Console.WriteLine (match. ToString ()); -             } in Console.readkey (); -         } to     } +}

Match by using match

The meaning of the 14 line is that the match begins with T, with one or more non-null characters in the middle (\s (uppercase) denotes any non-null character, + denotes one or more), followed by T, and finally with a space (\s (lowercase)), or a comma and a space, or a semicolon and space, or dot character. The same 22 lines, just changed to the heel X

The @ symbol in front of the quotation mark refers to the contents of the quotation mark are not escaped, so you do not need to add a \ before each \s \s.

15 lines MatchCollection coll1 = Reg. Matches (STR1);

The right side of the equal sign returns a matching set of str1 that matches the regular expression and assigns a value to the Matchcoll type Coll1

Results

C # Learning record 5--regular expressions Regular expression

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.