• c#正则表达式


    参考:http://www.cnblogs.com/sosoft/p/regex.html,做个记录

    (1)基本的语法字符

    d  0-9的数字
    D  d的补集(以所有字符为全集,下同),即所有非数字的字符
    w  单词字符,指大小写字母、0-9的数字、下划线
    W  w的补集
    s  空白字符,包括换行符 、回车符 、制表符 、垂直制表符v、换页符f
    S  s的补集
    .  除换行符/n外的任意字符
    […]  匹配[]内所列出的所有字符
    [^…]  匹配非[]内所列出的字符

    例子:

    string i = "
    ";
    string m = "3";
    Regex r = new Regex(@"D");
    //同Regex r = new Regex("\D");
    //r.IsMatch(i)结果:true
    //r.IsMatch(m)结果:false
    
    string i = "%";
    string m = "3";
    Regex r = new Regex("[a-z0-9]");
    //匹配小写字母或数字字符
    //r.IsMatch(i)结果:false
    //r.IsMatch(m)结果:true
    View Code

    (2)定位字符
    “定位字符”所代表的是一个虚的字符,它代表一个位置,你也可以直观地认为“定位字符”所代表的是某个字符与字符间的那个微小间隙。
    ^  表示其后的字符必须位于字符串的开始处
    $  表示其前面的字符必须位于字符串的结束处
      匹配一个单词的边界
    B  匹配一个非单词的边界
    另外,还包括:A  前面的字符必须位于字符处的开始处,z  前面的字符必须位于字符串的结束处,  前面的字符必须位于字符串的结束处,或者位于换行符前

    例子:

    string i = "Live for nothing,die for something";
    Regex r1 = new Regex("^Live for nothing,die for something$");
    //r1.IsMatch(i) true
    Regex r2 = new Regex("^Live for nothing,die for some$");
    //r2.IsMatch(i) false
    Regex r3 = new Regex("^Live for nothing,die for some");
    //r3.IsMatch(i) true
    
    string i = @"Live for nothing,
    die for something";//多行
    Regex r1 = new Regex("^Live for nothing,die for something$");
    Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0
    Regex r2 = new Regex("^Live for nothing,die for something$", RegexOptions.Multiline);
    Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//0
    Regex r3 = new Regex("^Live for nothing,/r/ndie for something$");
    Console.WriteLine("r3 match count:" + r3.Matches(i).Count);//1
    Regex r4 = new Regex("^Live for nothing,$");
    Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//0
    Regex r5 = new Regex("^Live for nothing,$", RegexOptions.Multiline);
    Console.WriteLine("r5 match count:" + r5.Matches(i).Count);//0
    Regex r6 = new Regex("^Live for nothing,/r/n$");
    Console.WriteLine("r6 match count:" + r6.Matches(i).Count);//0
    Regex r7 = new Regex("^Live for nothing,/r/n$", RegexOptions.Multiline);
    Console.WriteLine("r7 match count:" + r7.Matches(i).Count);//0
    Regex r8 = new Regex("^Live for nothing,/r$");
    Console.WriteLine("r8 match count:" + r8.Matches(i).Count);//0
    Regex r9 = new Regex("^Live for nothing,/r$", RegexOptions.Multiline);
    Console.WriteLine("r9 match count:" + r9.Matches(i).Count);//1
    Regex r10 = new Regex("^die for something$");
    Console.WriteLine("r10 match count:" + r10.Matches(i).Count);//0
    Regex r11 = new Regex("^die for something$", RegexOptions.Multiline);
    Console.WriteLine("r11 match count:" + r11.Matches(i).Count);//1
    Regex r12 = new Regex("^");
    Console.WriteLine("r12 match count:" + r12.Matches(i).Count);//1
    Regex r13 = new Regex("$");
    Console.WriteLine("r13 match count:" + r13.Matches(i).Count);//1
    Regex r14 = new Regex("^", RegexOptions.Multiline);
    Console.WriteLine("r14 match count:" + r14.Matches(i).Count);//2
    Regex r15 = new Regex("$", RegexOptions.Multiline);
    Console.WriteLine("r15 match count:" + r15.Matches(i).Count);//2
    Regex r16 = new Regex("^Live for nothing,/r$/n^die for something$", RegexOptions.Multiline);
    Console.WriteLine("r16 match count:" + r16.Matches(i).Count);//1
    //对于一个多行字符串,在设置了Multiline选项之后,^和$将出现多次匹配。
    
    string i = "Live for nothing,die for something";
    string m = "Live for nothing,die for some thing";
    Regex r1 = new Regex(@"/bthing/b");
    Console.WriteLine("r1 match count:" + r1.Matches(i).Count);//0
    Regex r2 = new Regex(@"thing/b");
    Console.WriteLine("r2 match count:" + r2.Matches(i).Count);//2
    Regex r3 = new Regex(@"/bthing/b");
    Console.WriteLine("r3 match count:" + r3.Matches(m).Count);//1
    Regex r4 = new Regex(@"/bfor something/b");
    Console.WriteLine("r4 match count:" + r4.Matches(i).Count);//1
    ///b通常用于约束一个完整的单词
    View Code

    (3)重复描述字符
    “重复描述字符”是体现C#正则表达式“很好很强大”的地方之一:
    {n}  匹配前面的字符n次
    {n,}  匹配前面的字符n次或多于n次
    {n,m}  匹配前面的字符n到m次
    ?  匹配前面的字符0或1次
    +  匹配前面的字符1次或多于1次
    *  匹配前面的字符0次或式于0次

    例子:

    string x = "1024";
    string y = "+1024";
    string z = "1,024";
    string a = "1";
    string b="-1024";
    string c = "10000";
    Regex r = new Regex(@"^+?[1-9],?d{3}$");
    Console.WriteLine("x match count:" + r.Matches(x).Count);//1
    Console.WriteLine("y match count:" + r.Matches(y).Count);//1
    Console.WriteLine("z match count:" + r.Matches(z).Count);//1
    Console.WriteLine("a match count:" + r.Matches(a).Count);//0
    Console.WriteLine("b match count:" + r.Matches(b).Count);//0
    Console.WriteLine("c match count:" + r.Matches(c).Count);//0
    //匹配1000到9999的整数。
    View Code
  • 相关阅读:
    Why Visual Studio Team System Isn't A LoadRunner Killer[转载]
    从键盘判断电脑主人
    LoadRunner中添加weblogic监视器(JMX)
    winrunner事务概念的代码应用(毫秒级)
    Delphi中的线程类
    换工作时如何提高自身的待遇(转贴)
    牛人是怎样用函数实现本地化测试的
    肯德基的见闻
    由 12306.cn 谈谈网站性能技术
    20个优秀的 CSS 网格系统(CSS Grid Systems)推荐
  • 原文地址:https://www.cnblogs.com/zhengwk/p/5301841.html
Copyright © 2020-2023  润新知