Regex类正则表达式应用笔记

在看Regex类 时记的笔记,都在注释中了http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex(VS.80).aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexApp
{
    
class Program
    {
        
//public static void Main()
        
//{

        
//    // Define a regular expression for currency values.
        
//   // Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
        
//    Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
        
//    // Define some test strings.
        
//    string[] tests = { "-42", "19.99", "0.001", "100 USD" };

        
//    // Check each test string against the regular expression.
        
//    foreach (string test in tests)
        
//    {
        
//        if (rx.IsMatch(test))
        
//        {
        
//            Console.WriteLine("{0} is a currency value.", test);
        
//        }
        
//        else
        
//        {
        
//            Console.WriteLine("{0} is not a currency value.", test);
        
//        }
        
//    }

        
//}    
        
//3.RegexOptions 枚举
 
//       Compiled 指定将正则表达式编译为程序集。这会产生更快的执行速度,但会增加启动时间。  
 
//CultureInvariant 指定忽略语言中的区域性差异。有关更多信息,请参见 在 RegularExpressions 命名空间中执行不区分区域性的操作。  
 
//ECMAScript 为表达式启用符合 ECMAScript 的行为。该值只能与 IgnoreCase、Multiline 和 Compiled 值一起使用。该值与其他任何值一起使用均将导致异常。  
 
//ExplicitCapture 指定有效的捕获仅为形式为 (?<name>) 的显式命名或编号的组。这使未命名的圆括号可以充当非捕获组,并且不会使表达式的语法 (?:) 显得笨拙。  
 
//IgnoreCase 指定不区分大小写的匹配。  
 
//IgnorePatternWhitespace 消除模式中的非转义空白并启用由 # 标记的注释。但是,IgnorePatternWhitespace 值不会影响或消除字符类中的空白。   
 
//Multiline 多行模式。更改 ^ 和 $ 的含义,使它们分别在任意一行的行首和行尾匹配,而不仅仅在整个字符串的开头和结尾匹配。  
 
//None 指定不设置选项。  
 
//RightToLeft 指定搜索从右向左而不是从左向右进行。  
 
//Singleline 指定单行模式。更改点 (.) 的含义,使它与每一个字符匹配(而不是与除 \n 之外的每个字符匹配)。  
        
// Define a regular expression for repeated words.
        
//Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
        
//  RegexOptions.Compiled | RegexOptions.IgnoreCase);
//        4.MatchCollection 类
//表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合

        
//public static void Main()
        
//{

        
//    // Define a regular expression for repeated words.
        
//    Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
        
//      RegexOptions.Compiled | RegexOptions.IgnoreCase);

        
//    // Define a test string.        
        
//    string text = "The the quick brown fox  fox jumped over the lazy dog dog.";

        
//    // Find matches.
        
//    MatchCollection matches = rx.Matches(text);
        
//     // Report the number of matches found.
        
//    Console.WriteLine("{0} matches found.", matches.Count);

        
//    // Report on each match.
        
//    foreach (Match match in matches)
        
//    {
        
//        string word = match.Groups["word"].Value;
        
//        int index = match.Index;
        
//        Console.WriteLine("{0} repeated at position {1}", word, index);
        
//    }

        
//}

//        5.MatchCollection 类
//表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合
//        C#
//[SerializableAttribute] 
//public delegate string MatchEvaluator (
//    Match match
//)

        
static void Main(string[] args)
        {
            
string sInput, sRegex;

            
// The string to search.
            sInput = "aabbccddeeffcccgghhcccciijjcccckkcc";

            
// A very simple regular expression.
            sRegex = "cc";

            Regex r 
= new Regex(sRegex);

            Program c 
= new Program();

            
// Assign the replace method to the MatchEvaluator delegate.
            MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceCC);

            
// Write out the original string.
            Console.WriteLine(sInput);

            
// Replace matched characters using the delegate method.
            sInput = r.Replace(sInput, myEvaluator);

            
// Write out the modified string.
            Console.WriteLine(sInput);
        }

        
public string ReplaceCC(Match m)
        
// Replace each Regex cc match with the number of the occurrence.
        {
            i
++;
            
return i.ToString() + i.ToString();
        }
        
public static int i = 0;


    }
}
原文地址:https://www.cnblogs.com/sendling/p/1434237.html