.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 构造函数准备了一个正则表达式以供编译

// This code example demonstrates the RegexCompilationInfo constructor
// and the Regex.CompileToAssembly() method.
// compile: csc genFishRegex.cs
 
namespace MyApp
{
    using System;
    using System.Reflection;
    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 类型对目标字符串进行匹配,并显示匹配项、组、捕获组以及匹配项在目标字符串中的索引位置。

// This code example demonstrates the RegexCompilationInfo constructor.
// Execute this code example after executing genFishRegex.exe.
// compile: csc /r:FishRegex.dll useFishRegex.cs
 
namespace MyApp
  {
  using System;
  using System.Reflection;
  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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/263825.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DOCKER - 容器抓包

https://help.aliyun.com/knowledge_detail/40564.html?spma2c4e.11153940.blogcont272172.10.b09e28a6AOdITp#Linux http://man7.org/linux/man-pages/man1/nsenter.1.html?spma2c4e.11153940.blogcont272172.9.b09e28a6AOdITp [转]待整理 nsenter nsenter 包含在绝大部分 L…

高仿QQ即时聊天软件开发系列之三登录窗口用户选择下拉框

上一篇高仿QQ即时聊天软件开发系列之二登录窗口界面写了一个大概的布局和原理 这一篇详细说下拉框的实现原理 先上最终效果图 一开始其实只是想给下拉框加一个placeholder效果&#xff0c;让下拉框在未选择未输入时显示一个提示字符串。由于Background对ComboBox无效&#xff0…

Nokia7610彩信设置

1. 进入功能表—工具—设置—连接设置 2. 进入接入点&#xff0c;按“选项”—“新增接入点”—“使用默认设置”。 3. 自定义连接名称&#xff0c;可为&#xff1a;中国移动彩信&#xff0c;数据承载方式&#xff1a;GPRS 接入点名称&#xff1a;cmwap 4. 用户名、提示输入…

matlab guidata两个,Matlab

%在控件本身函数中用hObject调用%在别的函数中&#xff0c;需要使用handles调用function varargout TestGUI(varargin)% TESTGUI MATLAB code for TestGUI.fig% TESTGUI, by itself, creates a new TESTGUI or raises the existing% singleton*.%% H TESTGUI …

spring boot jar包替换报错之Unable to open nested entry 'BOOT-INF/lib/cache-api-0.4.jar'.

spring boot用layout ZIP打出来的包能够支持外部classpath&#xff0c;但是当用rar/7zip替换其中的jar后&#xff0c;报下列错误&#xff1a; Unable to open nested entry BOOT-INF/lib/cache-api-0.4.jar. It has been compressed and nested jar files must be stored witho…

hadoop博客 oschina

http://my.oschina.net/Xiao629/blog?catalog449279

php用json交换二维数组,PHP和Javascript的JSON交互(处理一个二维数组)

我不得不承认&#xff1a;我是一个彻彻底底的JS白痴。但根据项目需要&#xff0c;不得不使用JSON&#xff0c;不管怎么说&#xff0c;经过一个晚上的学习&#xff0c;已经略有所成&#xff0c;记录下来。PHP的JSON类库我使用的是Services_JSON&#xff0c;没什么特别的优点&…

RoRoWoBlog 开源博客系统介绍

萝萝窝个人博客开源项目 以Asp.net MVC 2.0 ADO.Net Entity Framework 4.0 Unity 2.0 MvcPager JQuery 等技术框架&#xff0c;开发的个人博客系统。 支持MetaWeblog接口 通过MetaWeblog接口&#xff0c;可以将您个人博客系统中的博文&#xff0c;直接同步到您其它网站的博…

Python基础:模块化来搭项目

简单模块化 import 最好在最顶端sys.path.append("..")表示把当前程序所在位置向上提了一级在python3规范中&#xff0c;__init__.py并不是必须的。文件结构&#xff1a; . ├── utils │ ├── util.py │ └── class_util.py ├── src │ └── sub_…

(原)离开,只为更好的活着

序&#xff09;经过长时间的失眠&#xff0c;辗转反侧&#xff0c;开始默默的写下一篇文章&#xff0c;我不知道以后是怎样的方向&#xff0c;不过明天依旧会天亮。 入职&#xff09;那最初的梦想 有位朋友说&#xff0c;找工作一定不要找初创公司&#xff0c;那样你会疯狂的加…

Flask入门到放弃(四)—— 数据库

转载请在文章开头附上原文链接地址&#xff1a;https://www.cnblogs.com/Sunzz/p/10979970.html 数据库操作 ORM ORM 全拼Object-Relation Mapping&#xff0c;中文意为 对象-关系映射。主要实现模型对象到关系数据库数据的映射 优点 : 只需要面向对象编程, 不需要面向数据库编…

virtualbox安装centos6.5碰到的问题

今天无聊用virtualbox安装centos6.5 , 自己笔记本vm撑不住, 用公司的试试virtualbox先 安装快完成时 没有足够的内存配置kdump”&#xff08;在英文界面下提示的是“insufficient memory to configure kdump”&#xff09; 出现这个提示, 解决办法, 按这篇博客可以解决, 简单点…

matlab som聚类算法,使用SOM对数据进行聚类

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼6.7 2.5 5.8 1.8 37.2 3.6 6.1 2.5 36.5 3.2 5.1 2 36.4 2.7 5.3 1.9 36.8 3 5.5 2.1 35.7 2.5 5 2 35.8 2.8 5.1 …

程序从技术到管理:思维转变是关键

IT公司研发部门的管理人员大多是从公司内部的技术人员中提拔的。在快速发展的公司里&#xff0c;这样的机会更多。然而这种“半路出家”的转型也给我们带来了很多挑战&#xff0c;其中最关键的部分在于思维方式的转变。 从个人成就到团队成就。 无论是做管理还是做技术&#xf…

javascript技巧

1、作用域安全的构造函数 function Person(name,age){ if(this instanceof Person){ this.namename; this.ageage; this.getInfofunction (){}; }else{ new Person(name,age); } } 2、函数柯里化//使用闭包返回一个函数&#xff0c;函数的参数是外部函数传递内部自身函数的参数…

VLC 学习计划---文档阅读

一 videolan-howto-en-html 该文档完全描述了VideoLAN "流"的解决方法. VideoLAN 项目包括两个软件. 1) VLC:以前是视频流接收的客户端,但是现在也可以作为服务端工作.2) VLS:视频服务端,能发送 MPEG-1, MPEG-2 and MPEG-4 files, DVDs, digital satellite channels,…

php 重定向到https,php - 如何从HTTPS重定向到HTTP? - SO中文参考 - www.soinside.com

如果我了解您&#xff0c;以下代码将解决此问题&#xff1a;RewriteEngine OnRewriteCond %{HTTPS} offRewriteCond %{SCRIPT_FILENAME} !\/index\.php [NC]#the above line will exclude https://www.hellomysite.com/index.php# from the following rulesRewriteCond %{SCRIP…

JAVA 面试知识点

主要包括以下几个部分&#xff1a; Java 基础知识点Java 常见集合高并发编程&#xff08;JUC 包&#xff09;JVM 内存管理Java 8 知识点网络协议相关数据库相关MVC 框架相关大数据相关Linux 命令相关面试&#xff0c;是大家从学校走向社会的第一步。 互联网公司的校园招聘&…

rails中weill_paginate的paginate方法中不能使用额外参数的解决办法

我们知道高版本中的rails中的分页功能已经放在will_paginate这个gem中&#xff0c;我们在控制器方法中往往需要调用其paginate方法来实现分页数据集控制&#xff0c;举个例子&#xff1a;正常的情况我们想要每页显示10条记录可以这么写&#xff1a; Item.paginate(page:params[…

企业管理软件开发不能割裂各系统的功能

现今企业管理软件分类比较多&#xff0c;但在一个企业中可能随着自己的发展以及管理的需要&#xff0c;在不同时期会购买不同阶段的管理软件&#xff0c;出于各种考虑可能会买入不同厂商的软件系统&#xff0c;这样就带来各软件间的无缝接口问题&#xff0c;这个问题如不能及时…