zoukankan      html  css  js  c++  java
  • 学习笔记(4):C#中的正则简单总结

    1)Regex.IsMatch 判断是否匹配

    string str = "1234";
    bool result = Regex.IsMatch(str,"[0-9]{4}");

    ============================================================================

    2 ) Regex.Match 提取单个字符串

    string str = "1k2j3h2123jhb23";
    string str2 = (Regex.Match(str,"[0-9]+")).ToString();

    2.1) 单个字符串中的组提取(group)

    string str = "1k2j3h2123jhb23";
    Match match = Regex.Match(str, "([0-9]+)(.+)");
    string str1=match.Groups[1].Value;
    string str2 = match.Groups[2].Value;

    ===========================================================================

    3 )Regex.matches() 提取所有符合要求的字符串

    string str=@"绩大于80,则奖励50元.语文成绩等于100并且音乐成绩大于70,则奖励100元";
    MatchCollection matches = Regex.Matches(str, "[0-9]+"); //用 MatchCollection 类型的变量存储匹配到的字符

    foreach (Match mc in matches) //遍历输出得到的字符串
    {
    Console.WriteLine(mc.Value);
    }
    Console.ReadKey();


    ===============================================================================

    4)Regex.Replace 替换字符串

    string bir = "我的生日是05/21/2010耶";
    string result = Regex.Replace(bir,@"(\d{2})/(\d{2})/(\d{4})","$3-$1-$2");


    4.1)组替换
    string bir = "我的生日是05/21/2010耶";
    string result = Regex.Replace(bir,@"(\d{2})/(\d{2})/(\d{4})","$3-$1-$2");

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    g..gle                                                            . 表示任意单个字符

    he[ax]llo                                                        表示 hallo 或者 hxllo

    he[a-z]llo                                                      [a-z]表示a-z中的任意一个

    he[A-Za-z]                                                    [A-Za-z] 任意一个大写或者小写字母

    z|food                                                          zood 或者 food

    food*                                                           * 表示d出现 0 次 1 次或者多次

    food+                                                         + 表示d出现 1 次 或者多次

    food?                                                          ?表示d出现 1 次 或者 0次

    foo(d){8}                                                        d 出现 8 次
    foo(d){8,}                                                         d 出现 >8 次
    foo(d){8,10}                                                        d 出现 8-10 次

    he[^ax]llo                                                         [^ax]不是a且 不是x的任意一个字母

    \d                                                         数字
    \D                                                         非数字

    \s                                                         空白符
    \S                                                         非空白符

  • 相关阅读:
    【C语言】找出1000以内所有的素数
    【C语言】字符数组,碎碎念
    【C语言】将输入的10个数排序
    C语言 排序算法
    冒死透露!全球前25名最臭名昭着的黑客人物
    苹果系统新致命漏洞,黑客可以随意控制您的手机设备
    物流行业的5大安全风险
    黑客来势汹汹,数据科学能拯救社交媒体吗?
    Facebook超过1亿用户数据泄露,疑与中国黑客组织有关?
    太可怕了!黑客可以通过监控智能手机传感器窃取您的密码
  • 原文地址:https://www.cnblogs.com/key1309/p/3132490.html
Copyright © 2011-2022 走看看