Regular expressions Regex class common methods

Source: Internet
Author: User
1, IsMatch () method, the IsMatch () method is actually a return bool worthwhile method, if the test character satisfies the regular expression return true otherwise returns false.

Example:

Match the regular expression, minus @ does not affect the effect of the regex r = new Regex (@ "^[0-9]");//start matching match m = R.match (This.textBox1.Text); while (m.success) {       MessageBox.Show ("The first is the number");       return;}


Detects the value entered in the TextBox1, the first is not a number.

Small bet

1, IsMatch () method; the IsMatch () method is actually a return bool-worthy method, if the test character satisfies the regular expression return true otherwise returns false.

2. The @ symbol is used to output the @ "", and the contents of the middle of the two quotes are output as-is, regardless of the special symbol.

2, replace () method, the Replace () method is actually a replacement method, replacing the matching regular expression matching pattern.

Example:

Using system;using system.collections.generic;using system.linq;using system.text;using System.text.regularexpressions;namespace testregularexpressions{    class program    {        static void Main ( String[] args)       {            string regulartext = "\\w{1,}@\\w{1,}\\.";            String groupemail = "111@126.com";            if (Regex.IsMatch (groupemail,regulartext))            {                Console.WriteLine (Regex.Replace (Groupemail, "@", "= = ="));            }            Else            {                Console.WriteLine ("Not matched successfully! ");            }            Console.readkey ();}}}    

Output:

3, Split () method, Split () method is actually a split method, according to the matching regular expression of the split stored in the string array.

Example:

Using system;using system.collections.generic;using system.linq;using system.text;using System.text.regularexpressions;namespace testregularexpressions{    class program    {        static void Main ( String[] args)        {            string regulartext = ";";            String groupemail = "111@126.com;222@126.com;333@126.com;444@126.com;";            String[] Email;            Email = Regex.Split (Groupemail, regulartext);            foreach (String personemail in Email)            {                Console.WriteLine (personemail);            }            Console.readkey ();}}}    

Output:

Small bet

You can use the String.Split method for the string, which is the same effect. For example, to remove the vsnt in English, the code is as follows:

String[] Au=vsnt.split (', ');

Function specifics: Click to open link

Split function Small Package:

        #region split the string according to pattern///<summary>//        split string by pattern///</summary>//        <param Name= "Input" > string to Split </param>//        <param name= "pattern" > Split identifier </param>///        <returns > Split array </returns>        private string[] splitstring (string input, string pattern)        {            string[] Email;            Email = Regex.Split (input, pattern);            return Email;        }        #endregion

The above is the regular expression regex class common methods of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.