引用空间:System.Text.RegularExpression
在名字空间中仅仅包含着6个类和一个委托,它们是:
Capture: 包含一次匹配的结果;
CaptureCollection: Capture的序列;
Group: 一次组记录的结果,由Capture继承而来;
Match: 一次表达式的匹配结果,由Group继承而来;
MatchCollection: Match的一个序列;
MatchEvaluator: 执行替换操作时使用的代理;
Regex: 编译后的表达式的实例。
Regex类中还包含一些静态的方法:
Escape(): 对字符串中的regex中的转义符进行转义;
IsMatch(): 如果表达式在字符串中匹配,该方法返回一个布尔值;
Match(): 返回Match的实例;
Matches(): 返回一系列的Match的方法;
Replace(): 用替换字符串替换匹配的表达式;
Split(): 返回一系列由表达式决定的字符串;
Unescape():不对字符串中的转义字符转义。
六个类详解:
Capture:
// 摘要: 原始字符串中发现捕获的子字符串的第一个字符的位置。 // 返回结果:原始字符串中发现捕获的子字符串的从零开始的起始位置。 public int Index { get; } // 摘要: 捕获的子字符串的长度。 // 返回结果: 捕获的子字符串的长度。 public int Length { get; } // 摘要:从输入字符串中获取捕获的子字符串。 // 返回结果:通过匹配捕获的实际子字符串。 public string Value { get; } // 摘要:从输入字符串中获取捕获的子字符串。 // 返回结果: 通过匹配捕获的实际子字符串。 public override string ToString(); |
CaptureCollection:Capture的序列
// 摘要:获取由该组捕获的子字符串数。 //返回结果:CaptureCollection 中的项数。 public int Count { get; } // 摘要: i:捕获集合中的索引。 // 返回结果:位于集合中 i 位置的捕获子字符串。 public Capture this[int i] { get; } // 摘要:将集合的所有元素复制到给定数组array中,(从array的给定索引处开始插入)。 public void CopyTo(Array array, int arrayIndex); |
Group:
// 摘要:按从里到外、从左到右的顺序获取由捕获组匹配的所有捕获的集合(如果正则表达式用 System.Text.RegularExpressions.RegexOptions.RightToLeft。选项修改了,则顺序为按从里到外、从右到左)。该集合可以有零个或更多的项。 // 返回结果:由该组匹配的子字符串的集合。 public CaptureCollection Captures { get; } // 摘要:获取一个值,该值指示匹配是否成功。 // 返回结果:如果匹配成功,则为 true;否则为 false。 public bool Success { get; } // 摘要:返回一个与提供的对象等效的 Group 对象,在多个线程间共享该对象是安全的。 // 返回结果:一个正则表达式 Group 对象。这个多线程操作的时候比较重要 public static Group Synchronized(Group inner); |
Match:
{ // 摘要:获取空组。所有失败的匹配都返回此空匹配。 // 返回结果:一个空 System.Text.RegularExpressions.Match。 public static Match Empty { get; } // 摘要:获取由正则表达式匹配的组的集合。 // 返回结果:由模式匹配的字符组。 public virtual GroupCollection Groups { get; } // 摘要:从上一个匹配结束的位置开始返回一个包含下一个匹配结果的新 Match。 // 返回结果:下一个正则表达式 Match 对象。 public Match NextMatch(); // 摘要:返回对指定替换模式的扩展。 // 参数:replacement:要使用的替换模式。 // 返回结果:replacement 参数的扩展版本。 public virtual string Result(string replacement); // 摘要:返回一个与提供的实例等效的 Match 实例,该实例适合在多个线程间共享。 // 参数:inner:一个与预期的实例等效的 Match 实例。 // 返回结果:一个与提供的实例等效的 Match 实例,该实例适合在多个线程间共享。 public static Match Synchronized(Match inner); |
MatchCollection:
// 摘要:获取捕获数。 // 返回结果:捕获数。 public int Count { get; } // 摘要:获取该集合的i索引处的单个成员。 // 返回结果:位于集合中 i 位置的捕获子字符串。 public virtual Match this[int i] { get; } // 摘要:将集合的所有元素复制到给定数组array,从arrayIndex开始插入。 public void CopyTo(Array array, int arrayIndex); |
MatchEvaluator委托:
// 摘要:表示在 Overload:System.Text.RegularExpressions.Regex.Replace 方法操作过程中每当找到正则表达式匹配时都调用的方法。 // 返回结果: // 由 System.Text.RegularExpressions.MatchEvaluator 委托表示的方法返回的字符串。 [Serializable] public delegate string MatchEvaluator(Match match); |
Regex:
// 摘要:指示 System.Text.RegularExpressions.Regex 构造函数中指定的正则表达式在输入字符串中是否找到匹配项。 public bool IsMatch(string input); // 摘要:指示 System.Text.RegularExpressions.Regex 构造函数中指定的正则表达式从输入字符串的指定起始位置开始是否找到匹配项。 public bool IsMatch(string input, int startat); // 摘要:指示正则表达式使用 pattern 参数中指定的正则表达式是否在输入字符串中找到匹配项。 public static bool IsMatch(string input, string pattern); // 摘要:在指定的输入字符串中搜索 System.Text.RegularExpressions.Regex 构造函数中指定的正则表达式匹配项。 public Match Match(string input); // 摘要:从指定的输入字符串起始位置开始在输入字符串中搜索正则表达式匹配项。 public Match Match(string input, int startat); // 摘要:在指定的输入字符串中搜索 pattern 参数中提供的正则表达式的匹配项。 // 返回结果:一个正则表达式 System.Text.RegularExpressions.Match 对象。 public static Match Match(string input, string pattern); // // 摘要:从指定的输入字符串起始位置开始在输入字符串中搜索具有指定输入字符串长度的正则表达式匹配项。 public Match Match(string input, int beginning, int length); //此处也同上,具有一类类似方法 public MatchCollection Matches(string input); //在指定的输入字符串内,使用指定的替换字符串替换与某个正则表达式模式匹配的所有字符串。 public string Replace(string input, string replacement); //在指定的输入字符串内,使用 System.Text.RegularExpressions.MatchEvaluator 委托返回的字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。 public string Replace(string input, MatchEvaluator evaluator, int count); public static string Replace(string input, string pattern, MatchEvaluator evaluator); // 摘要: // 在由 System.Text.RegularExpressions.Regex 构造函数中指定的正则表达式定义的位置,将指定的输入字符串拆分指定的最大次数。 public string[] Split(string input, int count); |
在c#中实战:
替换:
public void Replace(){ string source = "刘备ABC关羽ABc张飞Abc赵云abc诸葛亮aBC孙权abC周瑜AbC鲁肃"; Regex regex = new Regex("abc", RegexOptions.IgnoreCase); string result = regex.Replace(source, "|"); } |
(转载别人的图片)
上图是需要注意的。这个正则表达式里面包含了四个group。默认为从左到右的方式匹配,所以查询得出来的最外面的MatchCollection 里面包含的每一个Match都是group[0]所示意的整个分组,对于Match.Groups又依次包含下面的几个子查询。其中索引为0的子查询其实就是本身。