zoukankan      html  css  js  c++  java
  • 用C#通过正则表达式截取字符串中符合条件的子字符串

    仅仅作为简单的记录,不多说直接上代码(仅测试使用):

            private void Test()
            { 
                Regex ConnoteA = new Regex("^[a-zA-Z]\d{8}$");
                Regex ConnoteAA = new Regex("^[a-zA-Z]{2}\d{7,10}$");
                Regex ConnoteAAA = new Regex("^[a-zA-Z]{3}\d{5,9}$");
                Regex ConnoteAAAA = new Regex("^[a-zA-Z]{4}[a-zA-Z0-9]{8}$");
                string haha = "w11111111";
                string xixi = "ww1111111";
                string hehe = "w111111111";
                string lala = "W11111111";
    
                string longlongstring = "w11111111222q33333333555555ee1234567890yyy111111111yyy000000AK123456Me123456tttt12345678";
    
                bool result = ConnoteA.IsMatch(haha);
                result = ConnoteA.IsMatch(xixi);
                result = ConnoteA.IsMatch(hehe);
                result = ConnoteA.IsMatch(lala);
    
                foreach (Match mch in ConnoteA.Matches(longlongstring))
                {
                    string x = mch.Value.Trim();
                }
    
                foreach (Match mch in Regex.Matches(longlongstring, "([a-zA-Z]\d{8})|([a-zA-Z]{2}\d{7,10})|([a-zA-Z]{3}\d{5,9})|([a-zA-Z]{4}[a-zA-Z0-9]{8})|((AK|AL|BU|CR|EL|HF|HU|SW|SY|WU|ME)\d{6})|([a-zA-Z]{4}\d{8}[A-Za-z0-9]{3}\d{4})"))
                {
                    string x = mch.Value.Trim();
                }
            }

    更多正则表达式的信息,请看如下链接:http://fineboy.cnblogs.com/archive/2005/09/08/232410.html

  • 相关阅读:
    JS学习专辑(3) DOM
    JS学习专辑(4) 变量作用域和语句
    JS学习专辑(6) 函数
    JS学习专辑(2) BOM
    WPF Adorner学习(1)
    C# 递归
    IEnumerable和IEnumerator
    C# 索引器
    JS学习专辑(5) 对象和数组
    JS学习专辑(1) 入门
  • 原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/3837887.html
Copyright © 2011-2022 走看看