• 正则表达式


    在线正则表达式

    UI:

    代码:

    private void button1_Click(object sender, EventArgs e)
            {
                //这个是正则表达式大全: https://www.cnblogs.com/hehehehehe/p/6043710.html 
                //1.
                //Regex reg = new Regex("^O.+A$");
    
                //if (reg.IsMatch(textBox1.Text))
                //{
                //    MessageBox.Show("OK");
                //}
                //else
                //{
                //    MessageBox.Show("NG");
                //}
    
                //2. 关于 ?的用法有两种,一、单独用 二、跟在其他符号后面,详情看上面的网址,有没有?的区别,可以看listBox1和listBox2
                //下面“+”的作用是匹配前面的(前面指的是 “.”,而“.”指的是匹配除“
    ”之外的任何“单个”字符,
                //所以“.+”的意思就是匹配OA之间一次或者多次的单个字符)一次或多次的子表达式
                Match mat = Regex.Match(textBox1.Text, "O.+?A");
                //MessageBox.Show(mat.Value.ToString());
    
                MatchCollection matS = Regex.Matches(textBox1.Text, "O.+?A");
                //MatchCollection matS = Regex.Matches(textBox1.Text, "O.*?A");
                listBox1.Items.Clear();
                foreach (Match item in matS)
                {
                    listBox1.Items.Add(item.Value.ToString());
                }
    
    
                MatchCollection matS1 = Regex.Matches(textBox1.Text, "O.+A");
                listBox2.Items.Clear();
                foreach (Match item in matS1)
                {
                    listBox2.Items.Add(item.Value.ToString());
                }
    
                //?指的是匹配前面的(前面指的是“.”)零次或者一次的子表达式,我的表达式是:a1OO23AAO45A67AA,所以下面的是空,
                //因为O和A之间的字符数至少都是2个字符
                MatchCollection matS3 = Regex.Matches(textBox1.Text, "O.?A");
                listBox3.Items.Clear();
                foreach (Match item in matS3)
                {
                    listBox3.Items.Add(item.Value.ToString());
                }
                //3.
                string aa = "";
                //下面*的意思是匹配前面的(指的是[0-9])0次或者多次的子表达式,所以表达式如果是12345a就不能通过,因为a不在[0-9]里面
                Regex reg1 = new Regex("^Chapter[1-9][0-9]*$");
    
                if (reg1.IsMatch(textBox1.Text))
                {
                    MessageBox.Show("OK");
                }
                else
                {
                    MessageBox.Show("NG");
                }
            }
  • 相关阅读:
    JVM内存模型
    052-224(新增70题2018)
    052-223(新增70题2018)
    052-222(新增70题2018)
    052-221(新增70题2018)
    052-220(新增70题2018)
    052-219(新增70题2018)
    052-218(新增70题2018)
    052-217(新增70题2018)
    052-216(新增70题2018)
  • 原文地址:https://www.cnblogs.com/ziqiumeng/p/10688285.html
Copyright © 2020-2023  润新知