摘抄自:http://blog.csdn.net/weiwenhp/article/details/7665816
Classes
Class | Description | |
---|---|---|
Capture | Represents the results from a single subexpression capture. Capture represents one substring for a single successful capture. | |
CaptureCollection | Represents a sequence of capture substrings. CaptureCollection returns the set of captures done by a single capturing group. | |
Group | Group represents the results from a single capturing group. A capturing group can capture zero, one, or more strings in a single match because of quantifiers, soGroup supplies a collection of Capture objects. | |
GroupCollection | Represents a collection of captured groups. GroupCollection returns the set of captured groups in a single match. | |
Match | Represents the results from a single regular expression match. | |
MatchCollection | Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string. | |
Regex | Represents an immutable regular expression. | |
RegexCompilationInfo | Provides information about a regular expression that is used to compile a regular expression to a stand-alone assembly. | |
RegexRunner | Infrastructure. The RegexRunner class is the base class for compiled regular expressions. | |
RegexRunnerFactory | Infrastructure. Creates a RegexRunner class for a compiled regular expression. |
Delegates
Delegate | Description | |
---|---|---|
MatchEvaluator | Represents the method that is called each time a regular expression match is found during a Replace method operation. |
Enumerations
Enumeration | Description | |
---|---|---|
RegexOptions | Provides enumerated values to use to set regular expression options. |
这些类里最主要的类其实是Regex,其他类都是在使用Regex类中的方法时而用到的.
类Regex类中有很多静态(static)方法,我们可以直接调用那些方法而不用实例化一个Regex类.但同时Regex类中还有与静态方法功能完全一样的实例方法.
就是说你可以通过实例化Regex后再去用.
是直接调用静态方法还是先实例化而调用实例方法其实没太大区别,只是传参数时稍微有一点点区别.至于你喜欢那一种方式,就任凭你自己的爱好了.
不过一般情况下如果只是少数几次使用正则静态式,一般用静态方法方便点.如果是频繁的使用正则表达式,则实例化类可能性能好点.下面举两个验证字符和提取字符简单的例子来说明用静态方法和实例方法的的细微区别
静态方法:
string str = "csdn.net/weiwenhp";
string pattern = "(?<=/).*(?=hp)"; //匹配/和hp之间的字符
bool exist = Regex.IsMatch(str, pattern); //验证下是否匹配成功
string result = Regex.Match(str, pattern).Value; //匹配到的值
if(exist)
Console.WriteLine(result); //结果是weiwen
实例方法 :
string str = "csdn.net/weiwenhp";
string pattern = "(?<=/).*(?=hp)";
Regex reg = new Regex(pattern); //实例化一个Regex类
bool exist = reg.IsMatch(str); //这里用法和静态方法基本一样,只不过实例化时用了参数pattern,这里就不再需要这参数了
string result = reg.Match(str).Value;
if(exist)
Console.WriteLine(result); //结果也同样是weiwen
下面介绍下Regex的Replace方法和代理MatchEvaluator
我们知道正则表达式主要是实现验证,提取,分割,替换字符的功能.Replace函数是实现替换功能的.
Replace函数有四个重载函数
1 )Replace(string input,string pattern,string replacement) //input是源字符串,pattern是匹配的条件,replacement是替换的内容,就是把符合匹配条件pattern的内容转换成它
比如string result = Regex.Replace("abc", "ab", "##"); //结果是##c,就是把字符串abc中的ab替换成##
2 )Replace(string input,string pattern,string replacement,RegexOptions options) //RegexOptions是一个枚举类型,用来做一些设定.
//前面用注释时就用到了RegexOptions.IgnorePatternWhitespace.如果在匹配时忽略大小写就可以用RegexOptions.IgnoreCase
比如string result = Regex.Replace("ABc", "ab", "##",RegexOptions.IgnoreCase);
如果是简单的替换用上面两个函数就可以实现了.但如果有些复杂的替换,比如匹配到很多内容,不同的内容要替换成不同的字符.就需要用到下面两个函数
3 )Replace(string input,string pattern,MatchEvaluator evaluator); //evaluator是一个代理,其实简单的说是一个函数指针,把一个函数做为参数参进来
//由于C#里没有指针就用代理来实现类似的功能.你可以用代理绑定的函数来指定你要实现的复杂替换.
4 )Replace(string input,string pattern,MatchEvaluator evaluator,RegexOptions options);//这个函数上上面的功能一样,只不过多了一点枚举类型来指定是否忽略大小写等设置
先来看个简单的例子
例子1
如果一个字符串中有两次出现arwen,我要把第一次出现的替换成weiwen(1),第二次出现的替换成weiwen(2)
public static int i = 0;
static void Main(string[] args)
{
string source = "##arwen***&&arwen###";
string pattern = "arwen";
string result;
MatchEvaluator me = Replace; //声明一个代理,并绑定一个函数.其实就相当于me是一个指向函数Replace的指针
result = Regex.Replace(source, pattern, me); //把me也可以看成是函数Replace当参数传进去
Console.WriteLine(result); //结果是"##weiwen(1)***&&weiwen(2)###";
}
public static string Replace(Match ma)
{
if (ma.Success)
{
i++;
return "weiwen" +string.Format("({0})",i);
}
return "NULL";
}
不过看了上面的代码还是有点晕,不知道它从头到尾是以什么样的顺序什么样的逻辑执行过来的.
result = Regex.Replace(source, pattern, me);//这看起来就一句话,其实它里面有很多复杂的操作,并且还有循环多次的操作
//首先会去这样匹配 MatchCollection matchs = Regex.Matches(source, pattern);由于会匹配到两个arwen.所以集合matchs中会有两个值
//matchs[0].Value = "arwen"; matchs[1] = "arwen". 然后会用for(int i = 0;i < matchs.matchs.Count;i++)
//{
// matchs[i] = Replace(matchs[i]); //这是这里把字符串替换了
//}
当然我上面说的只是大概解释下背后的大概流程应该是什么样的.
实现使用时我们只会用到result = Regex.Replace(source, pattern, me);这一句就OK了
例子2
上面说的是匹配相同的字符.那再举个例子说下匹配到不同的内容并做不同的替换
假如字符串中有数字2则替换成GOD,有数字3则替换成HELL.
string source = "aa2bb3cc";
string pattern = @"\d";
string result;
MatchEvaluator me = Replace;
result = Regex.Replace(source, pattern, me);
Console.WriteLine(result); //结果是aaGODbbHELLcc
public static string Replace(Match ma)
{
switch (ma.Value)
{
case "2":
return "GOD";
break;
case "3":
return "HELL";
break;
default:
return "NULL";
break;
}
}