分享
分享赚钱 收藏 举报 版权申诉 / 26

类型正则(二).ppt

  • 上传人:kuailexingkong
  • 文档编号:1199160
  • 上传时间:2018-06-18
  • 格式:PPT
  • 页数:26
  • 大小:725KB
  • 配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    正则(二).ppt
    资源描述:

    1、网络爬虫系列讲座,C#正则表达式应用,2,回顾,w、s、d、xn、un 含义?(), (?:), (?=), 含义?*, +, ?, n, n, n,m 限定符的作用?, $ 的作用?,3,回顾,实例:天气预报查询,实例:阿里巴巴供应商机搜索,实例:域名注册查询,4,本章目标,正则表达式在 C# 中的实现了解 C# 中正则对像、集合等。理解并编写常见的正则表达式,5,匹配词组并获取该匹配(在 C# 中称为“捕获组”):(Win98|Win2K|WinXP) 匹配某个文本中的 Win98 或 Win2K 或 WinXP 字符串 ,并获取该匹配 string input = 我的操作系统是 Win

    2、XP 专业版; string pattern = 我的操作系统是s*(Win98|Win2K|WinXP)s*(S+) 版; Match match = Regex.Match(input, pattern); Console.WriteLine(match.Value); Console.WriteLine(match.Groups1.Value); Console.WriteLine(match.Groups2.Value);输出:我的操作系统时 WinXP 专业版WinXP专业,正则表达式中的子表达式,6,正则对像在 System.Text.RegularExpressions 命名空间

    3、中,在该命名空间下放置着正则类、捕获结果类和捕获结果的集合类,以及委托和枚举。下面是最常用到的类:Regex 表示不可变的正则表达式RegexOptions 枚举,提供用于设置正则表达式选项的枚举值Match 表示单个正则表达式匹配的结果。MatchCollection 表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合 Group 表示单个捕获组的结果GroupCollection 表示捕获组的集合,单个匹配中的捕获组的集合,C# 中正则对像、集合等,7,Match 类。表示单个正则表达式匹配的结果,由于单个匹配可能涉及多个捕获组(子表达式),因此 Match 具有返回

    4、 GroupCollection 的 Groups 属性。MatchCollection 类。表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合 。我们调用 Regex.Matches 方法时,返回的就是 MatchCollection 类实例。,C# 中正则对像、集合等,8,Group 类。Group 类从 Capture 继承。表示单个捕获组的结果。由于存在数量词,一个捕获组可以在单个匹配中捕获零个、一个或更多的字符串,因此 Group 提供 Capture 对象的集合(即 Group 实例本身等效于由 Captures 属性返回的集合中的最后一项 )。GroupCo

    5、llection 类。表示捕获组的只读集合,返回单个匹配中的捕获组的集合。当我们使用子表达式捕获到结果时,就会放入到 GroupCollection 集合中。,C# 中正则对像、集合等,9,RegexOptions 枚举。Compiled 指定将正则表达式编译为程序集。这会产生更快的执行速度,但会增加启动时间。 CultureInvariant 指定忽略语言中的区域性差异。有关更多信息,请参见 在 RegularExpressions 命名空间中执行不区分区域性的操作。 ECMAScript 为表达式启用符合 ECMAScript 的行为。该值只能与 IgnoreCase、Multiline

    6、和 Compiled 值一起使用。该值与其他任何值一起使用均将导致异常。 ExplicitCapture 指定有效的捕获仅为形式为 (?.) 的显式命名或编号的组。这使未命名的圆括号可以充当非捕获组,并且不会使表达式的语法 (?:.) 显得笨拙。,C# 中正则对像、集合等,10,IgnoreCase 指定不区分大小写的匹配。 IgnorePatternWhitespace 消除模式中的非转义空白并启用由 # 标记的注释。但是IgnorePatternWhitespace 值不会影响或消除字符类中的空白。 Multiline 多行模式。更改 和 $ 的含义,使它们分别在任意一行的行首和行尾匹配,

    7、而不仅仅在整个字符串的开头和结尾匹配。 None 指定不设置选项。 RightToLeft 指定搜索从右向左而不是从左向右进行。 Singleline 指定单行模式。更改点 (.) 的含义,使它与每一个字符匹配(而不是与除 n 之外的每个字符匹配)。,C# 中正则对像、集合等,11,Regex 类。表示不可变的正则表达式。 我们可以创建 Regex 的实例也可以使用 Regex 的静态方法,执行正则匹配。Regex 的公共构造方法:Regex(string pattern) Regex(string pattern, RegexOptions options)Regex 常用的公共成员方法:I

    8、sMatch 指示正则表达式在输入字符串中是否找到匹配项 Match 搜索正则表达式的匹配项Matches 搜索正则表达式的所有匹配项Replace 使用指定字符串,替换正则表达式的匹配项Split 根据正则表达式匹配项,将字符串拆分为子字符串数组,C# 中正则对像、集合等,12,Regex 的公共构造方法:Regex(string pattern) 使用指定的正则表达式初始化 Regex 新实例正则对像的匹配选项为 RegexOptions.None 默认值Regex(string pattern, RegexOptions options)使用指定的正则表达式初始化 Regex 新实例用户

    9、指定匹配选项的 RegexOptions 的枚举数,C# 中正则对像、集合等,13,Regex.IsMatch 公共方法:bool IsMatch(string input);bool IsMatch(string input, int startat)static bool IsMatch(string input, string pattern);static bool IsMatch(string input, string pattern, RegexOptions options);,C# 中正则对像、集合等,14,Regex.Match 公共方法:Match Match(strin

    10、g input);Match Match(string input, int startat);Match Match(string input, int beginning, int length);static Match Match(string input, string pattern);static Match Match(string input, string pattern, RegexOptions options);,C# 中正则对像、集合等,15,Regex.Matches 公共方法:MatchCollection Matches(string input);Match

    11、Collection Matches(string input, int startat);static MatchCollection Match(string input, string pattern);static MatchCollection Match(string input, string pattern, RegexOptions options);,C# 中正则对像、集合等,16,Regex.Replace 公共方法:string Replace(string input, MatchEvaluator evaluator);string Replace(string i

    12、nput, string replacement);string Replace(string input, MatchEvaluator evaluator, int count);string Replace(string input, string replacement, int count);string Replace(string input, MatchEvaluator evaluator, int count, int startat);string Replace(string input, string replacement, int count, int start

    13、at),C# 中正则对像、集合等,17,Regex.Replace 公共方法:static string Replace(string input, string pattern, MatchEvaluator evaluator)static string Replace(string input, string pattern, string replacement)static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options);static string

    14、 Replace(string input, string pattern, string replacement, RegexOptions options);,C# 中正则对像、集合等,18,Regex.Split 公共方法:string Split(string input);string Split(string input, int count);string Split(string input, int count, int startat);static string Split(string input, string pattern);static string Split

    15、(string input, string pattern, RegexOptions options);,C# 中正则对像、集合等,19,验证 Email 地址Email 地址示例:分析:伊妹儿地址是由英文、数字、减号以及下滑线开始,并且随后有 符号分隔域名,域名通常有数字、字母、减号构成,并且至少有一个“.”号分隔一级域名,或由多个“.”号分隔多级域名(这里不考虑中文 Email 地址问题)。正则表达式为:w+(-.w+)*w+(-.w+)*.w+(-.w+)*w+(-.w+)*w+(-.w+)*.w+(-.w+)*,正则表达式的简单应用,规范,20,构造 Regex 对像,实现验证 Em

    16、ail 地址:/ 构造 Regex 对像实现:string email = ;string pattern = w+(-.w+)*w+(-.w+)*.w+(-.w+)*;Regex regex = new Regex(pattern);bool succeed = regex.IsMatch(email);if (succeed)Console.WriteLine(伊妹儿地址有效。);elseConsole.WriteLine(伊妹儿地址无效。);,C# 中应用正则对像,21,使用 Regex 静态方法,验证 Email 地址:/ 使用 Regex 静态方法实现:string email =

    17、;string pattern = w+(-.w+)*w+(-.w+)*.w+(-.w+)*;bool succeed = Regex.IsMatch(email, pattern);if (succeed)Console.WriteLine(伊妹儿地址有效。);elseConsole.WriteLine(伊妹儿地址无效。);,C# 中应用正则对像,22,提取单个 Email 地址:/ 使用 Regex 静态方法实现:string email = “、、;string pattern = w+(-.w+)*w+(-.w+)*.w+(-.w+)*;Match match = Regex.Matc

    18、h(email, pattern);if (match.Succceed)/ 提取 Email 成功Console.Write(找到伊妹儿地址,地址为: + match.Value);Console.Write(该地址在文本中的索引是: + match.Index);Console.WriteLine(地址的字符长度是:“ + match.Length);,C# 中应用正则对像,23,提取多个 Email 地址:/ 使用 Regex 静态方法实现:string email = “、、;string pattern = w+(-.w+)*w+(-.w+)*.w+(-.w+)*;MatchColl

    19、ection matches = Regex.Matches(email, pattern);if (matches.Count 0)/ 提取 Email 成功 foreach(Match match in matches) Console.WriteLine(找到伊妹儿地址,地址为: + match.Value); Console.WriteLine(该地址在文本中的索引是: + match.Index); Console.WriteLine(地址的字符长度是:“ + match.Length); ,C# 中应用正则对像,24,提取单个 Email 地址,并使用捕获组提取 Email 的邮箱

    20、名和域名:/ 使用 Regex 静态方法实现:string email = “、、;string pattern = (w+(-.w+)*)(w+(-.w+)*.w+(-.w+)*);Match match = Regex.Match(email, pattern);if (match.Succeed)/ 提取 Email 成功 Console.WriteLine(找到伊妹儿地址,地址为: + match.Value); Console.WriteLine(该伊妹儿地址在文本中的索引是: + match.Index); Console.WriteLine(地址的字符长度是:“ + match.

    21、Length); Console.WriteLine(“伊妹儿的邮箱名是:” + match.Groups1.Value); Console.WriteLine(“伊妹儿邮箱名在文本中的索引:” + match.Groups1.Index); Console.WriteLine(“伊妹儿邮箱名的长度:” + match.Groups1.Length); Console.WriteLine(“伊妹儿的域名是:” + match.Groups2.Value); Console.WriteLine(“伊妹儿的域名在文本中的索引:” + match.Groups2.Index); Console.Wr

    22、iteLine(“伊妹儿的域名的长度:” + match.Groups2.Length);,C# 中应用正则对像,25,C# 中使用 Regex 类,实现正则操作可以构造 Regex 实例,并调用实例方法可以使用 Regex 静态方法(通常使用静态方法)C# 中使用 Match、Group 类,分别存放捕获结果和捕获组结果C# MatchCollection、GroupCollection 集合类,分别存放多个 Match 实例和 Group 实例,总结,26,熟悉和使用正则工具 RegexBuddy 编写和测试正则表达式。仔细分析老师编写的获取天气预报 WinForm 应用程序,并自己尝试编写域名注册查询的应用程序。,作业,

    展开阅读全文
    提示  道客多多所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:正则(二).ppt
    链接地址:https://www.docduoduo.com/p-1199160.html
    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    道客多多用户QQ群:832276834  微博官方号:道客多多官方   知乎号:道客多多

    Copyright© 2025 道客多多 docduoduo.com 网站版权所有世界地图

    经营许可证编号:粤ICP备2021046453号    营业执照商标

    1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png



    收起
    展开