zoukankan      html  css  js  c++  java
  • C#正则的使用

    c#使用正则表达式要用到System.Text.RegularExprssions命名空间

    官方API

    Regex类是用于匹配表达式:

      通常Regex分为静态类和实例化俩种方式。那这俩种有什么区别呢,一般情况下使用静态的Regex的时候,.net会自动将正则表达式缓存起来,而在使用相同的时候正则的时候不会重新编译。一般情况下会缓存15个正则。而实例化一次Regex,则会重新编译正则。

    Match代表获取的正则结果:

      Match.value:代表获取正则内容

      Match.Index:代表内容的其实下标

      Match.Length:代表内容长度

      Match.Groups:代表正则所有捕获组,默认Group[0]为全部捕获内容,如果同一个捕获组有多个内容,则取最后一个

      Match.Captures如果一个捕获组有多个内容,Group代表最后一个,而Captures代表这个捕获组的全部内容  

      MatchCollection 一个可迭代的正则集合。

      Match.Success:判断是否匹配

      Match.Next:如果匹配到了多个结果,返回下一个

      Match.Result:替换模式,$1能够匹配到捕获组内容,对$1进行修饰如“--$1--”,则将匹配到的捕获组内容替换为--()--

    public class Example
    {
       public static void Main()
       {
          string pattern = "--(.+?)--";
          string replacement = "($1)";
          string input = "He said--decisively--that the time--whatever time it was--had come.";
          foreach (Match match in Regex.Matches(input, pattern))
          {
             string result = match.Result(replacement);
             Console.WriteLine(result);
          }
       }
    }
    // The example displays the following output:
    //       (decisively)
    //       (whatever time it was)

    Group:表示单个捕获组的结果正则中使用。而在正则中使用?:可以避免捕获组。

    Capture:表示单个成功子表达式捕获的结果。

    using System;
    using System.Text.RegularExpressions;
    
    public class Test
    {
    
        public static void Main ()
        {
    
            // Define a regular expression for repeated words.
            Regex rx = new Regex(@"(?<word>w+)s+(k<word>)",
              RegexOptions.Compiled | RegexOptions.IgnoreCase);
    
            // Define a test string.        
            string text = "The the quick brown fox  fox jumps over the lazy dog dog.";
            
            // Find matches.
            MatchCollection matches = rx.Matches(text);
    
            // Report the number of matches found.
            Console.WriteLine("{0} matches found in:
       {1}", 
                              matches.Count, 
                              text);
    
            // Report on each match.
            foreach (Match match in matches)
            {
                GroupCollection groups = match.Groups;
                Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                                  groups["word"].Value, 
                                  groups[0].Index, 
                                  groups[1].Index);
            }
            
        }
        
    }
    // The example produces the following output to the console:
    //       3 matches found in:
    //          The the quick brown fox  fox jumps over the lazy dog dog.
    //       'The' repeated at positions 0 and 4
    //       'fox' repeated at positions 20 and 25
    //       'dog' repeated at positions 50 and 54

      

      

  • 相关阅读:
    How to create jar for Android Library Project
    Very large tabs in eclipse panes on Ubuntu
    64bit Ubuntu, Android AAPT, R.java
    Linux(Ubuntu)下如何安装JDK
    Configure xterm Fonts and Colors for Your Eyeball
    建立、配置和使用Activity——启动其他Activity并返回结果
    建立、配置和使用Activity——使用Bundle在Activity之间交换数据
    建立、配置和使用Activity——启动、关闭Activity
    建立、配置和使用Activity——Activity
    异步任务(AsyncTask)
  • 原文地址:https://www.cnblogs.com/dlvguo/p/10018319.html
Copyright © 2011-2022 走看看