读书人

[转]C#正则表达式种Match和Group类的理

发布时间: 2012-09-28 00:03:35 作者: rapoo

[转]C#正则表达式类Match和Group类的理解

?

?

????? 正则表达式很重要的一个应用就是在文本中提取字符串,这一功能的实现主要是靠Match类和Group类,因此理解匹配和组的概念很重要。要实现在一段文本中查找URL功能,这个例子比较简单,只要调用Regex.Matches()方法就可以找到URL的集合。示例代码如下:

 public static void Main(string[] args)        {                       string text = "I've found this amazing URL at http://www.sohu.com ,and then find ftp://ftp.sohu.com is better.";            string pattern = @"\b(\S+)://(\S+)\b";  //匹配URL的模式            MatchCollection mc = Regex.Matches(text, pattern); //满足pattern的匹配集合            Console.WriteLine("文本中包含的URL地址有:");            foreach (Match match in mc)            {                Console.WriteLine(match.ToString());            }            Console.Read();        }        /*          * 运行后输出如下:         * 文本中包含的URL地址有:         * http://www.sohu.com         * ftp://ftp.sohu.com        */

? ? ? 现在,要求变了,不仅要找出URL,还要找出每个URL的协议和域名地址,这时就要用到正则表达式的分组功能了。分组是要匹配的模式pattern用小括号括起来,分成不同的组,如可以把上面例子中的模式改为:string pattern = @"\b(?<protocol>\S+)://(?<address>\S+)\b";?这样就用括号分成了两个组(实际上是三个组,因为匹配本身可以看做一个大组),"?<protocol>"和"?<address>"定义了每个组的别名protocol和address,这不是必须的,只是方便我们获取需要的组。示例代码如下:

?

    public static void Main(string[] args)    {        string text = "I've found this amazing URL at http://www.sohu.com ,and then find ftp://ftp.sohu.com is better.";        string pattern = @"\b(?<protocol>\S+)://(?<address>\S+)\b"; //匹配URL的模式,并分组        MatchCollection mc = Regex.Matches(text, pattern); //满足pattern的匹配集合        Console.WriteLine("文本中包含的URL地址有:");        foreach (Match match in mc)        {            GroupCollection gc = match.Groups;            string outputText = "URL:" + match.Value + ";Protocol:" + gc["protocol"].Value + ";Address:" + gc["address"].Value;            Console.WriteLine(outputText);        }        Console.Read();    }    /**     * 运行后输出如下:     * 文本中包含的URL地址有:     * URL:http://www.sohu.com;Protocol:http;Address:www.sohu.com     * URL:ftp://ftp.sohu.com;Protocol:ftp;Address:ftp.sohu.com    */

读书人网 >C#

热点推荐