C# 中使用正则表达式 Regex.Matches方法的几个应用[转]

C# 中使用正则表达式 Regex.Matches方法的几个应用[转]

用于正则表达式的 Regex.Matches静态方法的几种用法:

    //①正则表达式 = > 匹配字符串  
    string Text = @"This is a book , this is my book , Is not IIS";  
      
    //定义一个模式字符串,不仅仅是纯文本,还可以是正则表达式  
    string Pattern = "is";  
      
    MatchCollection Matches = Regex.Matches(  
        Text,  
        Pattern,  
        RegexOptions.IgnoreCase |         //忽略大小写  
        RegexOptions.ExplicitCapture |    //提高检索效率  
        RegexOptions.RightToLeft          //从左向右匹配字符串  
        );  
      
    Console.WriteLine("从右向左匹配字符串:");  
      
    foreach (Match NextMatch in Matches)  
    {                 
        Console.Write("匹配的位置:{0,2} ", NextMatch.Index);  
        Console.Write("匹配的内容:{0,2} ", NextMatch.Value);  
        Console.Write("/n");     
    }  
      
    Console.WriteLine();  
      
    //②匹配以大写I开头  
    //“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)  
    Pattern = @"/bI";  
    Matches = Regex.Matches(  
        Text,  
        Pattern,  
        RegexOptions.ExplicitCapture    //提高检索效率  
        );  
      
    Console.WriteLine("从左向右匹配字符串:");  
      
    foreach (Match NextMatch in Matches)  
    {  
        Console.Write("匹配的位置:{0} ", NextMatch.Index);  
        Console.Write("匹配的内容:{0} ", NextMatch.Value);  
        Console.Write("/n");  
    }  
      
    Console.WriteLine();  
      
    //③匹配以大写I开头,大写S结尾的字符串  
    //“/b”是转义序列,代表开头和结尾(一个字的边界,忽略空白或标点)  
    ///S*匹配任何不是空白的字符  
    Pattern = @"/bI/S*S/b";  
    Matches = Regex.Matches(  
        Text,  
        Pattern,  
        RegexOptions.ExplicitCapture    //提高检索效率  
        );  
      
    Console.WriteLine("从左向右匹配字符串:");  
      
    foreach (Match NextMatch in Matches)  
    {  
        Console.Write("匹配的位置:{0} ", NextMatch.Index);  
        Console.Write("匹配的内容:{0} ", NextMatch.Value);  
        Console.Write("/n");  
    }  
      
    Console.WriteLine();  
      
    //④匹配his 或者iis,其中忽略大小写  
    Pattern = @"[h|i]is";  
    Matches = Regex.Matches(  
        Text,  
        Pattern,  
        RegexOptions.IgnoreCase |         //忽略大小写  
        RegexOptions.ExplicitCapture    //提高检索效率  
        );  
      
    Console.WriteLine("从左向右匹配字符串:");  
      
    foreach (Match NextMatch in Matches)  
    {  
        Console.Write("匹配的位置:{0} ", NextMatch.Index);  
        Console.Write("匹配的内容:{0} ", NextMatch.Value);  
        Console.Write("/n");              
    }  
      
    Console.WriteLine();  
      
    //⑤对Url的分组匹配  
    Text = "http://192.168.0.1:2008";  
    Pattern = @"/b(/S+)://(/S+)(?::(/S+))/b";  
    Matches = Regex.Matches(Text, Pattern);  
      
    Console.WriteLine("从左向右匹配字符串:");  
      
    foreach (Match NextMatch in Matches)  
    {  
        Console.Write("匹配的位置:{0} ", NextMatch.Index);  
        Console.Write("匹配的内容:{0} ", NextMatch.Value);  
        Console.Write("/n");  
      
        for (int i = 0; i < NextMatch.Groups.Count; i++)  
        {  
            Console.Write("匹配的组 {0}:{1,4} ", i + 1, NextMatch.Groups[i].Value);  
            Console.Write("/n");  
        }  
    }  
      
    Console.Read();  

 输出结果为:

①从右向左匹配字符串:  
匹配的位置:43 匹配的内容:IS   
匹配的位置:35 匹配的内容:Is   
匹配的位置:22 匹配的内容:is   
匹配的位置:19 匹配的内容:is   
匹配的位置: 5 匹配的内容:is   
匹配的位置: 2 匹配的内容:is   
  
②从左向右匹配字符串:  
匹配的位置:35 匹配的内容:I   
匹配的位置:42 匹配的内容:I   
  
③从左向右匹配字符串:  
匹配的位置:42 匹配的内容:IIS   
  
④从左向右匹配字符串:  
匹配的位置:1 匹配的内容:his   
匹配的位置:18 匹配的内容:his   
匹配的位置:42 匹配的内容:IIS   
  
⑤从左向右匹配字符串:  
匹配的位置:0 匹配的内容:http://192.168.0.1:2008   
匹配的组 1:http://192.168.0.1:2008   
匹配的组 2:http   
匹配的组 3:192.168.0.1   
匹配的组 4:2008

—上善若水,随遇而安。
老子

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119289.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • eruka快速刷新和kafka配置

    eruka快速刷新和kafka配置Kafkapartition扩充并且需要迁移数据生产不用指定具体分区,会自动分配拉取也不用指定具体分区,会自动拉多个分区可以同时拉取多个topickafka配置<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka_2.12</artifactId><version>2.3.0</version>&lt…

    2022年8月21日
  • docker快速安装fastdfs服务springboot访问「建议收藏」

    docker快速安装fastdfs服务springboot访问「建议收藏」拉取镜像dockerpullmorunchang/fastdfs运行tracker跟踪器dockerrun-d–nametracker–net=hostmorunchang/fastdfsshtracker.sh运行storage存储器【注意:修改IP为自己的IP端口不变】dockerrun-d–namestorage–net=host-eTRACKER_IP=192.168.61.200:22122-eGROUP_NAME=gr

    2022年6月24日
  • java全局变量引起的并发问题「建议收藏」

    java全局变量引起的并发问题「建议收藏」最近刚完成了一个短彩信群发平台的开发工作,系统采用springmvc+hibernate+jdbctemplate+mysql架构。其中遇见许多问题,闲暇之余记录下来以避免在后续项目中再犯同样的错误。先看下面小段代码,一个controller,一个service。       controller.java代码:    ……..    @Autowired

    2022年8月21日
  • maven配置阿里云镜像仓库「建议收藏」

    maven配置阿里云镜像仓库「建议收藏」1.修改maven根目录下的conf文件夹中的setting.xml文件<mirrors><mirror><id>alimaven</id><name>aliyunmaven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorOf&g.

    2022年6月24日
  • 安卓ffmpeg_有什么好用的视频解码

    安卓ffmpeg_有什么好用的视频解码本文章是用ffmeg解码封装格式(如mp4)转换为yuv420p保存到本地,本文是结合雷霄骅博客ppt和某地方学习的一个笔记(说出来等下被认为做广告就尴尬了)封装格式视频编码数据将封装格式解压后可以得到压缩过的音视频等.将压缩过的视频解压后可以得到视频像素数据(RGB,YUV等).常见的视频压缩格式有H.264,MPEG4等…YUV420P格式介绍YUV是视频像素格式,在压缩视频格式解

    2022年9月25日
  • 在jsp页面将Date类型的日期显示成”yyyy-MM-dd HH:mm:ss”格式

    在jsp页面将Date类型的日期显示成”yyyy-MM-dd HH:mm:ss”格式头部加上:&lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt;内容中使用:&lt;fmt:formatDate value="${post.postDate }" pattern="yyyy-MM-dd HH:mm:ss"/&gt;或者&lt;fmt:formatDate value=&quot

    2022年6月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号