.NET中的正则表达式 (三)RegexCompilationInfo 类
RegexCompilationInfo 类
提供编译器用于将正则表达式编译为独立程序集的信息。
属性
IsPublic:获取或设置一个值,该值指示所编译的正则表达式是否具有公共可见性。
Name:获取或设置用于所编译的正则表达式的类型名称。
Namespace:获取或设置要将新类型添加到的命名空间。
Options:获取或设置编译正则表达式时使用的编译器选项。
Pattern:获取或设置要编译的正则表达式。
方法
Equals:已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
GetHashCode:用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)
GetType:获取当前实例的 Type。 (从 Object 继承。)
ReferenceEquals:确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
ToString:返回表示当前 Object 的 String。 (从 Object 继承。)
示例
面的代码示例通过三个步骤定义、创建和使用编译过的正则表达式。
第一个步骤将编译下面的代码示例。代码示例中的 RegexCompilationInfo 构造函数准备了一个正则表达式以供编译
1 | // This code example demonstrates the RegexCompilationInfo constructor |
2 | // and the Regex.CompileToAssembly() method. |
3 | // compile: csc genFishRegex.cs |
4 | |
5 | namespace MyApp |
6 | { |
7 | using System; |
8 | using System.Reflection; |
9 | using System.Text.RegularExpressions; |
10 | class GenFishRegEx |
11 | { |
12 | public static void Main() |
13 | { |
14 | // Pattern = Group matches one or more word characters, |
15 | // one or more white space characters, |
16 | // group matches the string "fish". |
17 | string pat = @"(/w+)/s+(fish)"; |
18 | |
19 | // Create the compilation information. |
20 | // Case-insensitive matching; type name = "FishRegex"; |
21 | // namespace = "MyApp"; type is public. |
22 | RegexCompilationInfo rci = new RegexCompilationInfo( |
23 | pat, RegexOptions.IgnoreCase, |
24 | "FishRegex", "MyApp", true); |
25 | |
26 | // Setup to compile. |
27 | AssemblyName an = new AssemblyName(); |
28 | an.Name = "FishRegex"; |
29 | RegexCompilationInfo[] rciList = { rci }; |
30 | |
31 | // Compile the regular expression. |
32 | Regex.CompileToAssembly(rciList, an); |
33 | } |
34 | } |
35 | } |
36 | |
37 | /* |
38 | This code example produces the following results: |
39 | |
40 | (Execute this code to generate the compiled regular |
41 | expression assembly named FishRegex.dll. |
42 | Use FishRegex.dll as a reference when compiling |
43 | useFishRegex.cs.) |
44 | |
45 | */ |
46 |
第二步:运行第一个步骤中编译的可执行文件。该可执行文件创建 FishRegex.dll 程序集以及一个名为 FishRegex 的编译过的正则表达式类型。
第三步:使用对 FishRegex.dll 的引用编译下面的代码示例,然后运行得到的可执行文件。该可执行文件使用 FishRegex 类型对目标字符串进行匹配,并显示匹配项、组、捕获组以及匹配项在目标字符串中的索引位置。
1 | // This code example demonstrates the RegexCompilationInfo constructor. |
2 | // Execute this code example after executing genFishRegex.exe. |
3 | // compile: csc /r:FishRegex.dll useFishRegex.cs |
4 | |
5 | namespace MyApp |
6 | { |
7 | using System; |
8 | using System.Reflection; |
9 | using System.Text.RegularExpressions; |
10 | |
11 | class UseFishRegEx |
12 | { |
13 | public static void Main() |
14 | { |
15 | // Match against the following target string. |
16 | string targetString = "One fish two fish red fish blue fish"; |
17 | int matchCount = 0; |
18 | FishRegex f = new FishRegex(); |
19 | |
20 | // Display the target string. |
21 | Console.WriteLine("/nInput string = /"" + targetString + "/""); |
22 | |
23 | // Display each match, capture group, capture, and match position. |
24 | foreach (Match m in f.Matches(targetString)) |
25 | { |
26 | Console.WriteLine("/nMatch(" + (++matchCount) + ")"); |
27 | for (int i = 1; i <= 2; i++) |
28 | { |
29 | Group g = m.Groups[i]; |
30 | Console.WriteLine("Group(" + i + ") = /"" + g + "/""); |
31 | CaptureCollection cc = g.Captures; |
32 | for (int j = 0; j < cc.Count; j++) |
33 | { |
34 | Capture c = cc[j]; |
35 | System.Console.WriteLine( |
36 | "Capture(" + j + ") = /"" + c + "/", Position = " + c.Index); |
37 | } |
38 | } |
39 | } |
40 | } |
41 | } |
42 | } |
43 | |
44 | /* |
45 | This code example produces the following results: |
46 | |
47 | Input string = "One fish two fish red fish blue fish" |
48 | |
49 | Match(1) |
50 | Group(1) = "One" |
51 | Capture(0) = "One", Position = 0 |
52 | Group(2) = "fish" |
53 | Capture(0) = "fish", Position = 4 |
54 | |
55 | Match(2) |
56 | Group(1) = "two" |
57 | Capture(0) = "two", Position = 9 |
58 | Group(2) = "fish" |
59 | Capture(0) = "fish", Position = 13 |
60 | |
61 | Match(3) |
62 | Group(1) = "red" |
63 | Capture(0) = "red", Position = 18 |
64 | Group(2) = "fish" |
65 | Capture(0) = "fish", Position = 22 |
66 | |
67 | Match(4) |
68 | Group(1) = "blue" |
69 | Capture(0) = "blue", Position = 27 |
70 | Group(2) = "fish" |
71 | Capture(0) = "fish", Position = 32 |
72 | |
73 | */ |
74 |
转载于:https://www.cnblogs.com/dyufei/archive/2010/08/14/2573922.html
.NET中的正则表达式 (三)RegexCompilationInfo 类相关推荐
- Java学习笔记——正则表达式(Pattern类、Matcher类和PatternSyntaxException)
目录 一.Pattern类 (一)Pattern 介绍 (二)Pattern 方法 二.Matcher类 (一)Matcher 类介绍 (二)Matcher 类方法 三.PatternSyntaxEx ...
- 【C#进阶二】C#中的正则表达式知识总结(字符转义/字符类/ 定位点/ 分组构造 /数量词/反向引用构造/替换构造/替代/正则表达式选项)(理论篇)
文章目录 0. 正则表达式网站推荐 1.字符转义 2.字符类 3. 定位点 4. 分组构造 5.数量词 6.反向引用构造 7.替换构造 8.替代 9.正则表达式选项 正则表达式是对字符串操作的一种逻辑 ...
- java正则表达式类_java中正则表达式之Pattern类与Matcher类
java中正则表达式之Pattern类与Matcher类 ======================================================================= ...
- java中的正则表达式类---表情转换实例
Java中的正则表达式类 ________________________________________ public interface MatchResult 匹配操作的结果. 此接口包含用于确 ...
- CImg库中CImg,CImgList,CImgDisplay三个类的介绍
转自:http://www.cppprog.com/2009/0426/108.html 本文简单介绍了CImg库中的三个大类:CImg,CImgList,CImgDisplay.然后给出了让CImg ...
- VBA中的正则表达式(三)
VBA中的正则表达式(三) --Pattern属性和Execute方法 1. Pattern属性 该属性没有固定的可选值,是设置正则表达式规则使用的. 2. 正则表达式中各符号代表的含义 字符 描述 ...
- 正则表达式--密码复杂度验证--必须包含大写、小写、数字、特殊字符中的至少三项
密码复杂度要求: 大写字母.小写字母.数字.特殊字符,四项中至少包含三项. import org.junit.Test; import org.springframework.util.StringU ...
- Swift中使用正则表达式
Swift中使用正则表达式 环境:swift3 xcode8 一.什么时候我们需要使用正则表达式 1.判断一些字符是否符合所需要求: 2.用于检索文本中所需字段. 二.正则表达式的语法 正则表达式的创 ...
- js 正则表达式奇偶字符串替换_Python中的正则表达式及其常用匹配函数用法简介...
今 日 鸡 汤 此曲只应天上有,人间难得几回闻. /1 前言/ 这次给大家主要是介绍Python中的正则表达式,及其相关函数的基本使用方法,并且捎带一些正则表达式给我们带来的便利. /2 简介/ Py ...
最新文章
- lists,tuples and sets of Python
- filter与servlet的比较
- ca证书 linux 导入_CA搭建与证书申请
- linux命令date
- 【Kaggle-MNIST之路】自定义程序结构(七)
- CentOS安装jdk(无需配置环境变量)
- 数字转字符函数_C语言常用的几个工具函数
- ubuntu19.10安装remarkable
- abc类ip地址_通信网络的IP地址分配原理
- 201709-5 除法 ccf(树状数组)
- mysql 常用命令与备份恢复 整理
- android if else语句,Android一起执行IF和ELSE语句
- PlaceHolder和Panel的区别【搜藏】
- java两人猜数字游戏,三人背后猜数字游戏
- 素数和 mooc 翁恺
- spring aop原理_源码解析Spring的AOP原理(一)
- java基础知识体系
- BZOJ 1901 Zju2112 Dynamic Rankings 题解
- python字典常见操作
- word_excel_office向程序发送命令时出现问题
热门文章
- 现金贷平台倒闭后,借的钱是否可以不还?
- 单片机小白学步系列(十八) 单片机/计算机系统概述:通信接口与协议
- foreach数组循环结构体
- python获取包下的所有对象_Python访问COM对象的comtypes包简介
- python安装缺少api怎么办_请问缺少win32api模块该如何解决?
- 判断按键值_ALIENTEK 阿波罗 STM32F767 开发板资料连载第七章 按键输入实验
- c语言获取系统剩余内存_C语言编程中的“堆”和“栈”七大不同之处
- linux找不到光口,以太坊查看命令_求助 输入ifconfig命令 后看不到eth0但是有eth3和eth4-CSDN论坛_区块链百科...
- CNN反向传播卷积核翻转
- 动态页面技术(JSP/EL/JSTL)