- .NET 指南
- 开始操作
- .NET 教程
- .NET 体系结构组件
- .NET Standard
- 什么是.NET 标准中的新增功能
- 目标框架
- .NET 术语表
- 使用 mdoc 生成文档
- .NET 的体系结构指南
- 使用 ASP.NET Core 和 Azure 构建新式 Web 应用程序
- 现代 Web 应用程序的特征
- 传统 Web 应用程序和单页面应用程序 (Spa) 之间进行选择
- 常见的客户端 Web 技术
- 开发 ASP.NET Core MVC 应用程序
- 使用 ASP.NET Core 应用中的数据
- 测试 ASP.NET Core MVC 应用程序
- Azure 的开发过程
- Azure 托管的 ASP.NET 核心 Web 应用的建议
- 使用 Azure 云和 Windows 容器更新现有 .NET 应用程序 (v1.0)
- 提升和移动现有应用 Azure IaaS
- 将关系数据库迁移到 azure
- 直接迁移现有应用 DevOps
- 到提起并移动现有的.NET 应用到云 DevOps 通用应用程序的原因
- 云 devops 通用应用程序中的 Microsoft 技术
- 云优化应用程序如何呢?
- 如何将现有的.NET 应用程序部署到 Azure App Service
- 将现有的.NET 应用程序部署为 Windows 容器
- 何时不将部署到 Windows 容器
- 何时在你的本地部署 Windows 容器 IaaS VM 基础结构
- 何时将 Windows 容器部署到 Azure Vm (IaaS 云)
- 何时到 Service Fabric 中部署 Windows 容器
- 何时将 Windows 容器部署到 Azure 容器服务 (即,Kubernetes)
- 构建可复原的服务供云: 采用在云中的暂时性故障
- 更新你的应用的监视和遥测
- 更新使用 CI/CD 管道和 DevOps 工具在云中的应用程序的生命周期
- 将迁移到混合云方案
- 演练和技术获取启动的概述
- 技术演练列表
- 技术演练列表
- 结论
- 容器和 Docker 简介
- 什么是 Docker?
- Docker 术语
- Docker 容器、 图像和注册表
- Docker 应用程序生命周期的介绍
- DevOps 协作基础的容器
- 有关适用于容器化应用的 Microsoft 平台和工具的简介
- 使用 Docker 和 Microsoft Azure 设计和开发容器化应用
- Docker 应用程序设计
- 常见容器设计原则
- 整体应用程序
- 状态和 Docker 应用程序中的数据
- SOA 应用程序
- 协调微服务和以高可伸缩性和可用性的 multicontainer 应用程序
- Docker 应用的开发环境
- 对于 Docker 应用程序的内部循环开发工作流
- 使用 Visual Studio Tools for Docker (在 Windows 上的 Visual Studio)
- 使用在 DockerFile 中的 Windows PowerShell 命令来设置 Windows 容器 (Docker 标准基于)
- 使用 Microsoft 工具的 Docker 应用程序 DevOps 工作流
- Docker 应用程序的外部循环 DevOps 工作流中的步骤
- 运行、管理和监视 Docker 生产环境
- 在生产环境中运行由和基于微服务的应用程序
- 管理生产 Docker 环境
- 监视容器化应用程序服务
- 关键结论
- .NET 微服务。 适用于容器化 .NET 应用程序的体系结构
- .NET 微服务。 适用于容器化 .NET 应用程序的体系结构
- 容器和 Docker 简介
- 什么是 Docker?
- Docker 术语
- Docker 容器、 图像和注册表
- 为 Docker 容器选择 .NET Core 还是 .NET Framework
- 一般性指导原则
- 何时选择 Docker 容器中的.NET 核心
- 何时选择 Docker 容器中的.NET Framework
- 决策表:.NET 框架,用于 Docker
- .NET 容器定向到何种操作系统
- 正式.NET Docker 映像
- 构建基于容器和微服务的应用程序
- Containerizing 整体应用程序
- 状态和 Docker 应用程序中的数据
- 面向服务的体系结构
- 微服务体系结构
- 每个微服务构成的数据自主性
- 与物理体系结构的逻辑体系结构
- 分布式的数据管理挑战和解决方案
- 标识每个微服务的域模型边界
- 直接与 API 网关模式的微服务构成客户端通信
- 在微服务体系结构的通信
- 基于消息的异步通信
- 创建、 不断发展,和版本控制 microservice Api 和协定
- 微服务可寻址性和服务注册表
- 创建复合 UI 基于微服务,包括 visual UI 形状和布局由多个微服务生成
- 复原和微服务中的高可用性
- 协调微服务和多容器应用程序的高可伸缩性和可用性
- 使用 Azure Service Fabric
- 基于 Docker 的应用程序的开发流程
- Docker 应用的开发工作流
- 在 Linux 或 Windows Nano Server 主机上部署基于单容器的 .NET Core Web 应用
- 将旧版整体式 .NET Framework 应用程序迁移到 Windows 容器
- 设计和开发基于微服务的多容器 .NET 应用程序
- 设计面向微服务构成的应用程序
- 创建简单的数据驱动 CRUD 微服务
- 定义与 docker-compose.yml 多容器应用程序
- 使用运行的容器的数据库服务器
- 实现基于事件的微服务 (集成事件) 之间的通信
- 实现使用 RabbitMQ 开发或测试环境事件总线
- 订阅事件
- 测试 ASP.NET 核心服务和 web 应用
- 使用 DDD 和 CQRS 模式降低微服务中的业务复杂性
- 应用中的微服务构成的简化的 CQRS 和 DDD 模式
- EShopOnContainers DDD 微服务中的应用 CQRS 和 CQS 方法
- 在 CQRS 微服务中实现读取/查询
- 设计 DDD 面向微服务
- 设计 microservice 域模型
- 实现 microservice 域模型与.NET 核心
- Seedwork (可重用基类,这些类和接口,您的域模型)
- 实现值对象
- 使用枚举类,而不枚举类型
- 设计域模型层中的验证
- 客户端验证 (验证在表示层)
- 域事件: 设计和实现
- 设计基础结构持久性层
- 实现与实体框架核心基础结构持久性层
- 作为持久性基础结构使用 NoSQL 数据库
- 设计微服务应用程序层和 Web API
- 实现微服务应用程序层使用 Web API
- 实现具有恢复能力的应用程序
- 处理部分失败
- 用于处理部分失败的策略
- 实现重试使用指数退让
- 实现弹性 Entity Framework 核心 SQL 连接
- 实现自定义 HTTP 调用的重试使用指数退让
- 使用 Polly 和实施 HTTP 调用重试使用指数退让
- 实现断路器模式
- 运行状况监视
- 保护 .NET 微服务和 Web 应用程序
- 有关在.NET 微服务和 web 应用程序的授权
- 在开发过程中安全地存储应用程序机密
- 使用 Azure 密钥保管库在生产时保护机密
- 记住的要点
- 为服务器应用选择 .NET Core 或 .NET Framework
- 什么是 托管代码 ?
- Automatic Memory Management
- 公共语言运行时 (CLR)
- 语言独立性和与语言无关的组件
- 语言独立性和与语言无关的组件
- 框架库
- .NET Framework 类库概述
- 在 .NET 中使用基类型
- 常规类型系统
- .NET Framework 中的类型转换
- .NET 中的类型转换表
- .NET 中的格式化类型
- 标准数字格式字符串
- 自定义数字格式字符串
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
- 标准 TimeSpan 格式字符串
- 自定义的 TimeSpan 格式字符串
- 枚举格式字符串
- 复合格式设置
- 执行格式设置操作
- 如何:用前导零填充数字
- 如何:从特定日期中提取星期几
- 如何:定义和使用自定义数值格式提供程序
- 如何:往返日期和时间值
- 如何:将用户在 Web 控件中输入的数值转换为数字
- 如何:向 Web 用户显示本地化的日期和时间信息
- 如何:显示日期和时间值中的毫秒
- 如何:用非公历日历显示日期
- 操作在.NET 中的字符串
- 在.NET 中使用字符串的最佳做法
- .NET 中的基本字符串操作
- 在.NET 中创建新的字符串
- 剪裁和移除从.NET 中的字符串的字符
- .NET 中的空白字符串
- 比较.NET 中的字符串
- 更改.NET 中的大小写
- 在.NET 中使用 StringBuilder 类
- 如何: 在.NET 中执行基本字符串操作
- .NET 正则表达式
- 正则表达式语言 - 快速参考
- 正则表达式中的字符转义
- 正则表达式中的字符类
- 正则表达式中的定位点
- 正则表达式中的分组构造
- 正则表达式中的限定符
- 正则表达式中的反向引用构造
- 正则表达式中的备用构造
- 正则表达式中的替代
- 正则表达式选项
- 正则表达式中的其他构造
- .NET 中的正则表达式的最佳实践
- 正则表达式对象模型
- 正则表达式行为的详细信息
- 正则表达式中的回溯
- 正则表达式中的编译和重复使用
- 正则表达式中的线程安全
- 正则表达式示例
- 正则表达式示例:扫描 HREF
- 正则表达式示例:更改日期格式
- 如何:从 URL 中提取协议和端口号
- 如何:从字符串中剥离无效字符
- 如何:确认字符串是有效的电子邮件格式
- 在.NET 中编码的字符
- 在.NET 中分析字符串
- NET 中分析数值字符串
- 分析日期和时间字符串.NET 中
- .NET 中分析其他字符串
- .NET 类库
- .NET 可移植性分析器
- 在 .NET 中处理和引发异常
- .NET 程序集文件格式
- 内存管理和.NET 中的垃圾回收
- 垃圾回收
- 垃圾回收的基础
- 垃圾回收和性能
- 被动回收
- 滞后时间模式
- 针对共享 Web 承载优化
- 垃圾回收通知
- 应用程序域资源监控
- 弱引用
- 泛型类型(泛型)概述
- 委托和 lambda
- LINQ(语言集成查询)
- 常规类型系统和公共语言规范
- 异步概述
- 深入了解异步
- 异步编程模式
- 基于任务的异步模式 (TAP)
- 实现基于任务的异步模式
- 使用基于任务的异步模式
- 与其他异步模式和类型互操作
- 基于事件的异步模式 (EAP)
- 使用基于事件的异步模式进行多线程编程
- 基于事件的异步模式概述
- 实现基于事件的异步模式
- 实现基于事件的异步模式的最佳做法
- 确定何时实现基于事件的异步模式
- 演练:实现支持基于事件的异步模式的组件
- 演练:实现支持基于事件的异步模式的组件
- 如何:实现基于事件的异步模式的客户端
- 如何:使用支持基于事件的异步模式的组件
- 异步编程模型 (APM)
- 使用 IAsyncResult 调用异步方法
- 使用 AsyncWaitHandle 阻止应用程序的执行
- 通过结束异步操作来阻止应用程序执行
- 轮询异步操作的状态
- 使用 AsyncCallback 委托结束异步操作
- 使用 AsyncCallback 委托和状态对象
- 使用委托进行异步编程
- 使用异步方式调用同步方法
- 本机互操作性
- 集合和数据结构
- .NET Framework 中的数字
- 日期、时间和时区
- 时区概述
- 在 DateTime、DateTimeOffset、TimeSpan 和 TimeZoneInfo 之间进行选择
- 查找本地系统上定义的时区
- 如何: 枚举计算机上存在的时区
- 如何: 访问预定义的 UTC 和当地时间区域对象
- 如何: 实例化 TimeZoneInfo 对象
- 实例化 DateTimeOffset 对象
- 如何: 创建不带调整规则的时区
- 如何: 创建带有调整规则的时区
- 保存和还原时区
- 如何: 将时区保存到嵌入的资源
- 如何: 从嵌入的资源还原时区
- 使用日历
- 使用日期和时间执行算术运算
- 如何: 在日期和时间运算中使用时区
- 在 DateTime 与 DateTimeOffset 之间进行转换
- 在各时区之间转换时间
- 如何: 解决不明确的时间
- 如何: 让用户解决不明确的时间
- 处理和引发事件
- 如何:引发和使用事件
- 如何:使用事件属性处理多个事件
- 如何:在 Web 窗体应用程序中使用事件
- 观察程序设计模式
- 观察程序设计模式最佳做法
- 如何:实现提供程序
- 如何:实现观察程序
- 托管执行过程
- 元数据和自描述组件
- 在 .NET Framework 中构建控制台应用程序
- .NET Framework 中的并行处理和并发
- .NET Framework 应用程序要点
- 文件和流 I/O
- 对 .NET Framework 应用程序进行全球化和本地化
- 全球化
- 本地化评审
- 本地化
- 不区分区域性的字符串操作
- 执行不区分区域性的字符串操作
- 执行不区分区域性的字符串比较
- 执行不区分区域性的大小写更改
- 在集合中执行不区分区域性的字符串操作
- 在数组中执行不区分区域性的字符串操作
- 开发全球通用应用程序的最佳做法
- 利用特性扩展元数据
- 应用特性
- 编写自定义特性
- 检索存储在特性中的信息
- 框架设计准则
- 命名准则
- 大小写约定
- 通用命名约定
- 程序集和 DLL 的名称
- 命名空间的名称
- 类、结构和接口的名称
- 类型成员的名称
- 命名参数
- 命名资源
- 类型设计准则
- 在类和结构之间选择
- 抽象类设计
- 静态类设计
- 接口设计
- 结构设计
- 枚举设计
- 嵌套类型
- 成员设计准则
- 成员重载
- 属性设计
- 构造函数设计
- 事件设计
- 字段设计
- 扩展方法
- 运算符重载
- 参数设计
- 扩展性设计
- 未密封类
- 受保护的成员
- 事件和回调
- 虚成员
- 抽象(抽象类型和接口)
- 用于实现抽象的基类
- 密封
- 异常设计准则
- 异常引发
- 使用标准异常类型
- 异常和性能
- 使用准则
- 数组
- 特性
- 集合准则
- 序列化
- System.Xml 使用情况
- 相等运算符
- 常见设计模式
- 依赖项属性
- 释放模式
- XML 文档和数据
- XML 处理选项
- 内存中 XML 数据处理
- 使用 DOM 模型处理 XML 数据
- XML 文档对象模型 (DOM)
- XML 节点类型
- XML 文档对象模型 (DOM) 层次结构
- 将对象层次结构映射到 XML 数据
- 创建 XML 文档
- 将 XML 文档读入 DOM
- 嵌入到文档中的样式表指令
- 从读取器中加载数据
- 加载 DOM 时的空白和有效空白处理
- 访问 DOM 中的属性
- 将实体声明和实体引用读入 DOM
- 保留实体引用
- 扩展但不保留实体引用
- 将节点插入 XML 文档中
- 在 DOM 中创建新节点
- 为 DOM 中的元素创建新属性
- 创建新节点时的 XML 元素和属性名验证
- 创建新实体引用
- 命名空间对包含元素和属性的新节点的实体引用扩展的影响
- 复制现有节点
- 将现有节点从一个文档复制到另一个文档
- 复制文档片段
- 移除 XML 文档中的节点、内容和值
- 从 DOM 中移除节点
- 移除 DOM 中元素节点的属性
- 移除 DOM 中的节点内容
- 修改 XML 文档中的节点、内容和值
- 在 DOM 中验证 XML 文档
- 保存和写出文档
- 使用 XPath 导航选择节点
- 解析外部资源
- 使用 XmlNameTable 的对象比较
- NamedNodeMap 和 NodeList 中的节点集合
- 按名称或索引检索未排序节点
- 按索引检索已排序节点
- NodeList 和 NamedNodeMap 的动态更新
- DOM 中的命名空间支持
- DOM 中的命名空间和 DTD
- 更改 XML 文档中的命名空间声明
- 更改命名空间前缀属性
- 使用 mlNodeChangedEventArgs 的 XML 文档中的事件处理
- 扩展 DOM
- 使用 XPath 数据模型处理 XML 数据
- 使用 XPathDocument 和 XmlDocument 读取 XML 数据
- 使用 XPathNavigator 选择、计算和匹配 XML 数据
- 使用 XPathNavigator 选择 XML 数据
- 使用 XPathNavigator 计算 XPath 表达式
- 使用 XPathNavigator 匹配节点
- XPath 查询识别的节点类型
- XPath 查询和命名空间
- 已编译的 XPath 表达式
- XPath 命名空间浏览
- 使用 XPathNavigator 访问 XML 数据
- 使用 XPathNavigator 的节点集定位
- 使用 XPathNavigator 的属性和命名空间节点定位
- 使用 XPathNavigator 提取 XML 数据
- 使用 XPathNavigator 访问强类型 XML 数据
- 用户定义的函数和变量
- 使用 XPathNavigator 编辑 XML 数据
- 使用 XPathNavigator 插入 XML 数据
- 使用 XPathNavigator 修改 XML 数据
- 使用 XPathNavigator 移除 XML 数据
- 使用 XPathNavigator 验证架构
- 使用 LINQ to XML 处理 XML 数据
- XSLT 转换
- 使用 XslCompiledTransform 类
- XslCompiledTransform 类的输入
- XslCompiledTransform 类的输出选项
- 在 XSLT 处理期间解析外部资源
- 扩展 XSLT 样式表
- XSLT 扩展对象
- XSLT 参数
- 使用 msxsl:script 的脚本块
- 可恢复的 XSLT 错误
- 如何:转换节点片断
- 从 XslTransform 类迁移
- 如何:迁移 XslTransform 代码
- XSLT 安全注意事项
- XSLT 编译器 (xsltc.exe)
- 如何:通过使用程序集执行 XSLT 转换
- XslTransform 类的 XSLT 转换
- XslTransform 类中任意行为的实现
- XslTransform 类实现 XSLT 处理器
- XslTransform 的输出
- 不同存储区的 XSLT 转换
- 解析外部 XSLT 样式表和文档
- 样式表参数和扩展对象的 XsltArgumentList
- XSLT 样式表脚本使用<msxsl: script>
- 对 msxsl:node-set() 函数的支持
- 转换中的节点集
- 转换中的结果树片断
- 转换中的 XPathNavigator
- 转换中的 XPathNodeIterator
- XslTransform 的 XPathDocument 输入
- XslTransform 的 XmlDataDocument 输入
- XslTransform 的 XmlDocument 输入
- 使用 XML 架构
- XML 架构对象模型 (SOM)
- XML 架构对象模型概述
- 读写 XML 架构
- 生成 XML 架构
- 遍历 XML 架构
- 编辑 XML 架构
- 包含或导入 XML 架构
- 用于编译架构的 XmlSchemaSet
- 后架构编译信息集
- 使用 XmlSchemaSet 进行 XML 架构 (XSD) 验证
- XmlSchemaCollection 架构编译
- 使用 XmlSchemaCollection 进行 XDR 验证
- 使用 XmlSchemaCollection 进行 XML 架构 (XSD) 验证
- XmlSchemaValidator 基于推送的验证
- 推断 XML 架构
- 从 XML 文档推断架构
- 推断架构节点类型和结构的规则
- 推断简单类型的规则
- 关系数据和 ADO.NET 的 XML 集成
- 管理 XML 文档中的命名空间
- System.Xml 类中的类型支持
- 将 XML 数据类型映射到 CLR 类型
- XML 类型支持实现说明
- XML 数据类型的转换
- 将字符串转换为 .NET Framework 数据类型
- 将 .NET Framework 类型转换为字符串
- 托管线程处理
- 托管线程处理基本知识
- 线程与线程处理
- 托管线程中的异常
- 为多线程处理同步数据
- 托管线程状态
- 前台和后台线程
- Windows 中的托管和非托管线程处理
- Thread.Suspend、垃圾回收和安全点
- 线程本地存储区:线程相关的静态字段和数据槽
- 托管线程中的取消
- 如何:通过轮询侦听取消请求
- 如何:注册取消请求的回调
- 如何:侦听具有等待句柄的取消请求
- 如何:侦听多个取消请求
- 使用线程和线程处理
- 启动时创建线程并传递数据
- 暂停和继续线程
- 销毁线程
- 调度线程
- 以协作方式取消线程
- 托管线程处理的最佳做法
- 线程处理对象和功能
- 托管线程池
- 计时器
- EventWaitHandle、AutoResetEvent、CountdownEvent、ManualResetEvent
- EventWaitHandle
- AutoResetEvent
- ManualResetEvent 和 ManualResetEventSlim
- CountdownEvent
- Mutexes
- 互锁操作
- 读取器/编写器锁
- Semaphore 和 SemaphoreSlim
- 同步基元概述
- 屏障 (.NET Framework)
- 如何:使用屏障来使并发操作保持同步
- SpinLock
- 如何:使用 SpinLock 进行低级别同步
- 如何:在 SpinLock 中启用线程跟踪模式
- SpinWait
- 如何:使用 SpinWait 实现两阶段等待操作
- .NET 中的并行编程
- 任务并行库 (TPL)
- 数据并行(任务并行库)
- 如何:编写简单的 Parallel.For 循环
- 如何:编写简单的 Parallel.ForEach 循环
- 如何:编写具有线程局部变量的 Parallel.For 循环
- 如何:编写具有线程局部变量的 Parallel.ForEach 循环
- 如何:取消 Parallel.For 或 ForEach Loop
- 如何:处理并行循环中的异常
- 如何:加快小型循环体的速度
- 如何:使用并行类循环访问文件目录
- 基于任务的异步编程
- 使用延续任务来链接任务
- 已附加和已分离的子任务
- 任务取消
- 异常处理(任务并行库)
- 如何:使用 Parallel.Invoke 来执行并行操作
- 如何:从任务中返回值
- 如何:取消任务及其子级
- 如何:创建预先计算的任务
- 如何:使用并行任务遍历二叉树
- 如何:解除嵌套任务的包装
- 如何:防止子任务附加到其父任务
- 数据流(任务并行库)
- 如何:将消息写入数据流块和从数据流块读取消息
- 如何:实现制造者-使用者数据流模式
- 如何:在数据流块收到数据时执行操作
- 演练:创建数据流管道
- 如何:取消链接数据流块
- 演练:在 Windows 窗体应用程序中使用数据流
- 如何:取消数据流块
- 演练:创建自定义数据流块类型
- 如何:使用 JoinBlock 从多个源读取数据
- 如何:指定数据流块中的并行度
- 如何:在数据流块中指定任务计划程序
- 演练:使用 BatchBlock 和 BatchedJoinBlock 提高效率
- 将 TPL 用于其他异步模式
- TPL 和传统 .NET Framework 异步编程
- 如何:在任务中包装 EAP 模式
- 数据并行和任务并行中的潜在缺陷
- 并行 LINQ (PLINQ)
- PLINQ 介绍
- 了解 PLINQ 中的加速
- PLINQ 中的顺序保留
- PLINQ 中的合并选项
- PLINQ 的潜在缺陷
- 如何:创建并执行简单的 PLINQ 查询
- 如何:在 PLINQ 查询中控制排序
- 如何:合并并行和顺序 LINQ 查询
- 如何:处理 PLINQ 查询中的异常
- 如何:取消 PLINQ 查询
- 如何:编写自定义 PLINQ 聚合函数
- 如何:在 PLINQ 中指定执行模式
- 如何:在 PLINQ 中指定合并选项
- 如何:使用 PLINQ 循环访问文件目录
- 如何:衡量 PLINQ 查询性能
- PLINQ 数据示例
- 用于并行编程的数据结构
- 并行诊断工具
- PLINQ 和 TPL 的自定义分区程序
- 如何:实现动态分区
- 如何:实现静态分区的分区程序
- PLINQ 和 TPL 中的 Lambda 表达式
- 其他阅读材料(并行编程)
- .NET Framework 中的安全性
- 安全性的基础概念
- 基于角色的安全性
- 主体和标识对象
- 如何:创建 WindowsPrincipal 对象
- 如何:创建 GenericPrincipal 和 GenericIdentity 对象
- 替换 Principal 对象
- 模拟与恢复
- .NET Framework 加密模型
- 加密服务
- 生成加密和解密的密钥
- 如何:将非对称密钥存储在密钥容器中
- 加密数据
- 解密数据
- 加密签名
- 使用哈希代码确保数据完整性
- 创建加密方案
- 如何:用对称密钥对 XML 元素进行加密
- 如何:用对称密钥对 XML 元素进行解密
- 如何:用非对称密钥对 XML 元素进行加密
- 如何:用非对称密钥对 XML 元素进行解密
- 如何:用 X.509 证书对 XML 元素进行加密
- 如何:用 X.509 证书对 XML 元素进行解密
- 如何:使用数字签名为 XML 文档签名
- 如何:验证 XML 文档的数字签名
- 如何:使用数据保护
- 如何:访问硬件加密设备
- 演练:创建加密应用程序
- 代码安全维护指南
- 保护状态数据
- 安全性和用户输入
- 安全和争用条件
- 安全性和进行中的代码生成
- .NET 中的序列化
- 序列化帮助主题
- 二进制序列化
- 序列化概念
- 基本序列化
- 有选择的序列化
- 自定义序列化
- 序列化过程中的步骤
- 版本容错序列化
- 序列化准则
- 如何:对序列化数据进行分块
- 如何: 确定是否可序列化的标准.NET 对象
- 请参阅
- 请参阅
- XML 和 SOAP 序列化
- 如何:控制派生类的序列化
- XML 序列化简介
- 如何:反序列化对象
- XML 序列化示例
- XML 架构定义工具和 XML 序列化
- How to: Use the XML Schema Definition Tool to Generate Classes and XML Schema Documents
- 使用属性控制 XML 序列化
- 用来控制 XML 序列化的属性
- 如何:指定 XML 流的替代元素名称
- 如何:序列化对象
- 如何:限定 XML 元素和 XML 属性名
- 使用 XML Web services 进行 XML 序列化
- 如何:将对象序列化为 SOAP 编码的 XML 流
- 如何:重写编码的 SOAP XML 序列化
- 用来控制编码的 SOAP 序列化的属性
- <system.xml.serialization> 元素
- <dateTimeSerialization> 元素
- <schemaImporterExtensions> 元素
- <xmlSerializer> 元素
- 序列化工具
- XML 序列化程序生成器工具 (Sgen.exe)
- XML Schema Definition Tool (Xsd.exe)
- .NET Framework 的序列化示例
- 基本序列化技术示例
- 使用 XmlSerializer 自定义序列化顺序
- SchemaImporterExtension 技术示例
- 版本容错序列化技术示例
- Web 服务泛型序列化技术示例
- Web 服务 IXmlSerializable 技术示例
- 使用 .NET Framework 针对多个平台开发
- 使用可移植类库的跨平台开发
- 将可移植类库与模型-视图-视图模型配合使用
- 面向多个平台的库的应用程序资源
- .NET Framework 对 Windows 应用商店应用程序和 Windows 运行时的支持情况
- 向 Windows 运行时传递 URI
- WindowsRuntimeStreamExtensions.AsRandomAccessStream(System.IO.Stream) 方法
正则表达式对象模型
本主题介绍在使用.NET 正则表达式中使用的对象模型。 它包含下列部分:
正则表达式引擎
.NET 中的正则表达式引擎由 Regex 类。 正则表达式引擎负责分析和编译正则表达式,并执行用于将正则表达式模式与输入字符串相匹配的操作。 引擎是.NET 正则表达式对象模型中的核心组件。
可以通过以下两种方式之一使用正则表达式引擎:
- 通过调用 Regex 类的静态方法。 方法参数包含输入字符串和正则表达式模式。 正则表达式引擎会缓存静态方法调用中使用的正则表达式,这样一来,重复调用使用同一正则表达式的静态正则表达式方法将提供相对良好的性能。
- 通过实例化 Regex 对象,采用的方式是将一个正则表达式传递给类构造函数。 在此情况下, Regex 对象是不可变的(只读),它表示一个与单个正则表达式紧密耦合的正则表达式引擎。 由于未对 Regex 实例使用的正则表达式进行缓存,因此不应使用同一正则表达式实例化 Regex 对象多次。
可以调用 Regex 类的方法来执行下列操作:
- 确定字符串是否与正则表达式模式匹配。
- 提取单个匹配项或第一个匹配项。
- 提取所有匹配项。
- 替换匹配的子字符串。
- 将单个字符串拆分成一个字符串数组。
以下各部分对这些操作进行了描述。
匹配正则表达式模式
如果字符串与此模式匹配,则 Regex.IsMatch 方法返回 true
;如果字符串与此模式不匹配,则该方法返回 false
。 IsMatch 方法通常用于验证字符串输入。 例如,下面的代码将确保字符串与有效的美国社会保障号匹配。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string[] values = { "111-22-3333", "111-2-3333"};
string pattern = @"^\d{3}-\d{2}-\d{4}$";
foreach (string value in values) {
if (Regex.IsMatch(value, pattern))
Console.WriteLine("{0} is a valid SSN.", value);
else
Console.WriteLine("{0}: Invalid", value);
}
}
}
// The example displays the following output:
// 111-22-3333 is a valid SSN.
// 111-2-3333: Invalid
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim values() As String = { "111-22-3333", "111-2-3333"}
Dim pattern As String = "^\d{3}-\d{2}-\d{4}$"
For Each value As String In values
If Regex.IsMatch(value, pattern) Then
Console.WriteLine("{0} is a valid SSN.", value)
Else
Console.WriteLine("{0}: Invalid", value)
End If
Next
End Sub
End Module
' The example displays the following output:
' 111-22-3333 is a valid SSN.
' 111-2-3333: Invalid
正则表达式模式 ^\d{3}-\d{2}-\d{4}$
的含义如下表所示。
模式 | 说明 |
---|---|
^ | 匹配输入字符串的开头部分。 |
\d{3} | 匹配三个十进制数字。 |
- | 匹配连字符。 |
\d{2} | 匹配两个十进制数字。 |
- | 匹配连字符。 |
\d{4} | 匹配四个十进制数字。 |
$ | 匹配输入字符串的末尾部分。 |
提取单个匹配项或第一个匹配项
Regex.Match 方法返回一个 Match 对象,该对象包含有关与正则表达式模式匹配的第一个子字符串的信息。 如果 Match.Success
属性返回 true
,则表示已找到一个匹配项,可以通过调用 Match.NextMatch 方法来检索有关后续匹配项的信息。 这些方法调用可以继续进行,直到 Match.Success
属性返回 false
。 例如,下面的代码使用 Regex.Match(String, String) 方法查找重复的单词在字符串中的第一个匹配项。 然后,此代码调用 Match.NextMatch 方法查找任何其他匹配项。 该示例将在每次调用方法后检查 Match.Success
属性以确定当前匹配是否成功,并确定是否应接着调用 Match.NextMatch 方法。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "This is a a farm that that raises dairy cattle.";
string pattern = @"\b(\w+)\W+(\1)\b";
Match match = Regex.Match(input, pattern);
while (match.Success)
{
Console.WriteLine("Duplicate '{0}' found at position {1}.",
match.Groups[1].Value, match.Groups[2].Index);
match = match.NextMatch();
}
}
}
// The example displays the following output:
// Duplicate 'a' found at position 10.
// Duplicate 'that' found at position 22.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "This is a a farm that that raises dairy cattle."
Dim pattern As String = "\b(\w+)\W+(\1)\b"
Dim match As Match = Regex.Match(input, pattern)
Do While match.Success
Console.WriteLine("Duplicate '{0}' found at position {1}.", _
match.Groups(1).Value, match.Groups(2).Index)
match = match.NextMatch()
Loop
End Sub
End Module
' The example displays the following output:
' Duplicate 'a' found at position 10.
' Duplicate 'that' found at position 22.
正则表达式模式 \b(\w+)\W+(\1)\b
的含义如下表所示。
模式 | 描述 |
---|---|
\b | 在单词边界处开始匹配。 |
(\w+) | 匹配一个或多个单词字符。 这是第一个捕获组。 |
\W+ | 匹配一个或多个非单词字符。 |
(\1) | 与第一个捕获的字符串匹配。 这是第二个捕获组。 |
\b | 在单词边界处结束匹配。 |
提取所有匹配项
Regex.Matches 方法返回一个 MatchCollection 对象,该对象包含有关正则表达式引擎在输入字符串中找到的所有匹配项的信息。 例如,可重写上一示例以调用 Matches 方法,而不是调用 Match 和 NextMatch 方法。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "This is a a farm that that raises dairy cattle.";
string pattern = @"\b(\w+)\W+(\1)\b";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("Duplicate '{0}' found at position {1}.",
match.Groups[1].Value, match.Groups[2].Index);
}
}
// The example displays the following output:
// Duplicate 'a' found at position 10.
// Duplicate 'that' found at position 22.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "This is a a farm that that raises dairy cattle."
Dim pattern As String = "\b(\w+)\W+(\1)\b"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("Duplicate '{0}' found at position {1}.", _
match.Groups(1).Value, match.Groups(2).Index)
Next
End Sub
End Module
' The example displays the following output:
' Duplicate 'a' found at position 10.
' Duplicate 'that' found at position 22.
替换匹配的子字符串
Regex.Replace 方法会将与正则表达式模式匹配的每个子字符串替换为指定的字符串或正则表达式模式,并返回进行了替换的整个输入字符串。 例如,下面的代码在字符串的十进制数字前添加了美国货币符号。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\b\d+\.\d{2}\b";
string replacement = "$$$&";
string input = "Total Cost: 103.64";
Console.WriteLine(Regex.Replace(input, pattern, replacement));
}
}
// The example displays the following output:
// Total Cost: $103.64
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b\d+\.\d{2}\b"
Dim replacement As String = "$$$&"
Dim input As String = "Total Cost: 103.64"
Console.WriteLine(Regex.Replace(input, pattern, replacement))
End Sub
End Module
' The example displays the following output:
' Total Cost: $103.64
正则表达式模式 \b\d+\.\d{2}\b
的含义如下表所示。
模式 | 描述 |
---|---|
\b | 在单词边界处开始匹配。 |
\d+ | 匹配一个或多个十进制数字。 |
\. | 匹配句点。 |
\d{2} | 匹配两个十进制数字。 |
\b | 在单词边界处结束匹配。 |
替换模式 $$$&
的含义如下表所示。
模式 | 替换字符串 |
---|---|
$$ | 美元符号 ($) 字符。 |
$& | 整个匹配的子字符串。 |
将单个字符串拆分成一个字符串数组
Regex.Split 方法在由正则表达式匹配项定义的位置拆分输入字符串。 例如,下面的代码将编号列表中的项置于字符串数组中。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "1. Eggs 2. Bread 3. Milk 4. Coffee 5. Tea";
string pattern = @"\b\d{1,2}\.\s";
foreach (string item in Regex.Split(input, pattern))
{
if (! String.IsNullOrEmpty(item))
Console.WriteLine(item);
}
}
}
// The example displays the following output:
// Eggs
// Bread
// Milk
// Coffee
// Tea
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "1. Eggs 2. Bread 3. Milk 4. Coffee 5. Tea"
Dim pattern As String = "\b\d{1,2}\.\s"
For Each item As String In Regex.Split(input, pattern)
If Not String.IsNullOrEmpty(item) Then
Console.WriteLine(item)
End If
Next
End Sub
End Module
' The example displays the following output:
' Eggs
' Bread
' Milk
' Coffee
' Tea
正则表达式模式 \b\d{1,2}\.\s
的含义如下表所示。
模式 | 描述 |
---|---|
\b | 在单词边界处开始匹配。 |
\d{1,2} | 匹配一个或两个十进制数字。 |
\. | 匹配句点。 |
\s | 与空白字符匹配。 |
MatchCollection 和 Match 对象
Regex 方法返回作为正则表达式对象模型的一部分的两个对象: MatchCollection 对象和 Match 对象。
Match 集合
Regex.Matches 方法返回一个 MatchCollection 对象,该对象包含多个 Match 对象,这些对象表示正则表达式引擎在输入字符串中找到的所有匹配项(其顺序为这些匹配项在输入字符串中的显示顺序)。 如果没有匹配项,则此方法将返回一个不包含任何成员的 MatchCollection 对象。 利用 MatchCollection.Item[Int32] 属性,你可以按照索引(从零到将 MatchCollection.Count 属性的值减 1 所得的值)访问集合中的各个成员。 Item[Int32] 是集合的索引器(在 C# 中)和默认属性(在 Visual Basic 中)。
默认情况下,调用 Regex.Matches 方法会使用延迟计算来填充 MatchCollection 对象。 访问需要完全填充的集合的属性(如 MatchCollection.Count 和 MatchCollection.Item[Int32] 属性)可能会降低性能。 因此,建议你使用由 IEnumerator 方法返回的 MatchCollection.GetEnumerator 对象访问该集合。 各种语言都提供构造,如 For``Each
在 Visual Basic 中和 foreach
在 C# 中,用于包装该集合的 IEnumerator 接口。
下面的示例使用 Regex.Matches(String) 方法将在输入字符串中找到的所有匹配项填充到 MatchCollection 对象中。 此示例枚举了该集合,将匹配项复制到字符串数组并将字符位置记录在整数数组中。
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
MatchCollection matches;
List<string> results = new List<string>();
List<int> matchposition = new List<int>();
// Create a new Regex object and define the regular expression.
Regex r = new Regex("abc");
// Use the Matches method to find all matches in the input string.
matches = r.Matches("123abc4abcd");
// Enumerate the collection to retrieve all matches and positions.
foreach (Match match in matches)
{
// Add the match string to the string array.
results.Add(match.Value);
// Record the character position where the match was found.
matchposition.Add(match.Index);
}
// List the results.
for (int ctr = 0; ctr < results.Count; ctr++)
Console.WriteLine("'{0}' found at position {1}.",
results[ctr], matchposition[ctr]);
}
}
// The example displays the following output:
// 'abc' found at position 3.
// 'abc' found at position 7.
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim matches As MatchCollection
Dim results As New List(Of String)
Dim matchposition As New List(Of Integer)
' Create a new Regex object and define the regular expression.
Dim r As New Regex("abc")
' Use the Matches method to find all matches in the input string.
matches = r.Matches("123abc4abcd")
' Enumerate the collection to retrieve all matches and positions.
For Each match As Match In matches
' Add the match string to the string array.
results.Add(match.Value)
' Record the character position where the match was found.
matchposition.Add(match.Index)
Next
' List the results.
For ctr As Integer = 0 To results.Count - 1
Console.WriteLine("'{0}' found at position {1}.", _
results(ctr), matchposition(ctr))
Next
End Sub
End Module
' The example displays the following output:
' 'abc' found at position 3.
' 'abc' found at position 7.
Match 类
Match 类表示单个正则表达式匹配项的结果。 可以通过两种方式访问 Match 对象:
- 通过从 MatchCollection 方法返回的 Regex.Matches 对象检索这些对象。 若要检索各个 Match 对象,请通过使用
foreach
(在 C# 中)或For Each
...Next
(在 Visual Basic 中)构造循环访问集合;或者使用 MatchCollection.Item[Int32] 属性以按索引或名称检索特定的 Match 对象。 也可以通过按索引(从零到将集合中的对象数减去 1 所得的值)循环访问集合来检索集合中的各个 Match 对象。 但是,此方法不使用延迟计算,因为它将访问 MatchCollection.Count 属性。下面的示例通过使用 Match 或 MatchCollection ...
foreach
构造循环访问集合,来从For Each
对象中检索各个Next
对象。 正则表达式只是与输入字符串中的字符串abc
匹配。using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = "abc"; string input = "abc123abc456abc789"; foreach (Match match in Regex.Matches(input, pattern)) Console.WriteLine("{0} found at position {1}.", match.Value, match.Index); } } // The example displays the following output: // abc found at position 0. // abc found at position 6. // abc found at position 12.
Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "abc" Dim input As String = "abc123abc456abc789" For Each match As Match In Regex.Matches(input, pattern) Console.WriteLine("{0} found at position {1}.", _ match.Value, match.Index) Next End Sub End Module ' The example displays the following output: ' abc found at position 0. ' abc found at position 6. ' abc found at position 12.
- 通过调用 Regex.Match 方法,此方法返回一个 Match 对象,该对象表示字符串中的第一个匹配项或字符串的一部分。 可以通过检索
Match.Success
属性的值确定是否已找到匹配项。 若要检索表示后续匹配项的 Match 对象,请重复调用 Match.NextMatch 方法,直到返回的Success
对象的 Match 属性为false
。下面的示例使用 Regex.Match(String, String) 和 Match.NextMatch 方法来匹配输入字符串中的字符串
abc
。using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = "abc"; string input = "abc123abc456abc789"; Match match = Regex.Match(input, pattern); while (match.Success) { Console.WriteLine("{0} found at position {1}.", match.Value, match.Index); match = match.NextMatch(); } } } // The example displays the following output: // abc found at position 0. // abc found at position 6. // abc found at position 12.
Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "abc" Dim input As String = "abc123abc456abc789" Dim match As Match = Regex.Match(input, pattern) Do While match.Success Console.WriteLine("{0} found at position {1}.", _ match.Value, match.Index) match = match.NextMatch() Loop End Sub End Module ' The example displays the following output: ' abc found at position 0. ' abc found at position 6. ' abc found at position 12.
Match 类的以下两个属性都将返回集合对象:
- Match.Groups 属性返回一个 GroupCollection 对象,该对象包含有关与正则表达式模式中的捕获组匹配的子字符串的信息。
Match.Captures
属性返回一个 CaptureCollection 对象,该对象的使用是有限制的。 不会为其 Match 属性为Success
的false
对象填充集合。 否则,它将包含一个 Capture 对象,该对象具有的信息与 Match 对象具有的信息相同。
有关这些对象的更多信息,请参见本主题后面的 组集合 和 捕获集合 部分。
Match 类的另外两个属性提供了有关匹配项的信息。 Match.Value
属性返回输入字符串中与正则表达式模式匹配的子字符串。 Match.Index
属性返回输入字符串中匹配的字符串的起始位置(从零开始)。
Match 类还具有两个模式匹配方法:
- Match.NextMatch 方法查找位于由当前的 Match 对象表示的匹配项之后的匹配项,并返回表示该匹配项的 Match 对象。
- Match.Result 方法对匹配的字符串执行指定的替换操作并返回相应结果。
下面的示例使用 Match.Result 方法在每个包含两个小数位的数字前预置一个 $ 符号和一个空格。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\b\d+(,\d{3})*\.\d{2}\b";
string input = "16.32\n194.03\n1,903,672.08";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Result("$$ $&"));
}
}
// The example displays the following output:
// $ 16.32
// $ 194.03
// $ 1,903,672.08
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b\d+(,\d{3})*\.\d{2}\b"
Dim input As String = "16.32" + vbCrLf + "194.03" + vbCrLf + "1,903,672.08"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine(match.Result("$$ $&"))
Next
End Sub
End Module
' The example displays the following output:
' $ 16.32
' $ 194.03
' $ 1,903,672.08
正则表达式模式 \b\d+(,\d{3})*\.\d{2}\b
的定义如下表所示。
模式 | 描述 |
---|---|
\b | 在单词边界处开始匹配。 |
\d+ | 匹配一个或多个十进制数字。 |
(,\d{3})* | 匹配零个或多个以下模式:一个逗号后跟三个十进制数字。 |
\. | 匹配小数点字符。 |
\d{2} | 匹配两个十进制数字。 |
\b | 在单词边界处结束匹配。 |
替换模式 $$ $&
指示匹配的子字符串应由美元符号 ($)( $$
模式)、空格和匹配项的值( $&
模式)替换。
组集合
Match.Groups 属性返回一个 GroupCollection 对象,该对象包含多个 Group 对象,这些对象表示单个匹配项中的捕获的组。 集合中的第一个 Group 对象(位于索引 0 处)表示整个匹配项。 此对象后面的每个对象均表示一个捕获组的结果。
可以使用 Group 属性检索集合中的各个 GroupCollection.Item[String] 对象。 可以在集合中按未命名组的序号位置来检索未命名组,也可以按命名组的名称或序号位置来检索命名组。 未命名捕获将首先在集合中显示,并将按照未命名捕获在正则表达式模式中出现的顺序从左至右对它们进行索引。 在对未命名捕获进行索引后,将按照命名捕获在正则表达式模式中出现的顺序从左至右对它们进行索引。 若要确定在特定的正则表达式匹配方法返回的集合中哪些编号的组可用,可以调用实例 Regex.GetGroupNumbers 方法。 若要确定集合中哪些命名的组可用,可以调用实例 Regex.GetGroupNames 方法。 这两种方法在分析通过任何正则表达式找到的匹配的常规用途例程中都特别有用。
GroupCollection.Item[String] 属性是集合的索引器(在 C# 中)和集合对象的默认属性(在 Visual Basic 中)。 这表示可以按索引(对于命名组,可以按名称)访问各个 Group 对象,如下所示:
Group group = match.Groups[ctr];
Dim group As Group = match.Groups(ctr)
下面的示例定义一个正则表达式,该表达式使用分组构造捕获日期的年、月和日部分。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\b(\w+)\s(\d{1,2}),\s(\d{4})\b";
string input = "Born: July 28, 1989";
Match match = Regex.Match(input, pattern);
if (match.Success)
for (int ctr = 0; ctr < match.Groups.Count; ctr++)
Console.WriteLine("Group {0}: {1}", ctr, match.Groups[ctr].Value);
}
}
// The example displays the following output:
// Group 0: July 28, 1989
// Group 1: July
// Group 2: 28
// Group 3: 1989
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "\b(\w+)\s(\d{1,2}),\s(\d{4})\b"
Dim input As String = "Born: July 28, 1989"
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then
For ctr As Integer = 0 To match.Groups.Count - 1
Console.WriteLine("Group {0}: {1}", ctr, match.Groups(ctr).Value)
Next
End If
End Sub
End Module
' The example displays the following output:
' Group 0: July 28, 1989
' Group 1: July
' Group 2: 28
' Group 3: 1989
正则表达式模式 \b(\w+)\s(\d{1,2}),\s(\d{4})\b
的定义如下表所示。
模式 | 描述 |
---|---|
\b | 在单词边界处开始匹配。 |
(\w+) | 匹配一个或多个单词字符。 这是第一个捕获组。 |
\s | 与空白字符匹配。 |
(\d{1,2}) | 匹配一个或两个十进制数字。 这是第二个捕获组。 |
, | 匹配逗号。 |
\s | 与空白字符匹配。 |
(\d{4}) | 匹配四个十进制数字。 这是第三个捕获组。 |
\b | 在单词边界处结束匹配。 |
捕获的组
Group 类表示来自单个捕获组的结果。 表示正则表达式中定义的捕获组的组对象由 Item[String] 属性所返回的 GroupCollection 对象的 Match.Groups 属性返回。 Item[String] 属性是索引器(在 C# 中)和 Group 类的默认属性(在 Visual Basic 中)。 您还可以通过循环访问集合使用来检索各个成员 foreach
或 For``Each
构造。 有关示例,请参见上一部分。
下面的示例使用嵌套的分组构造来将子字符串捕获到组中。 正则表达式模式 (a(b))c
将匹配字符串 abc
。 它会将子字符串 ab
分配给第一个捕获组,并将子字符串 b
分配给第二个捕获组。
List<int> matchposition = new List<int>();
List<string> results = new List<string>();
// Define substrings abc, ab, b.
Regex r = new Regex("(a(b))c");
Match m = r.Match("abdabc");
for (int i = 0; m.Groups[i].Value != ""; i++)
{
// Add groups to string array.
results.Add(m.Groups[i].Value);
// Record character position.
matchposition.Add(m.Groups[i].Index);
}
// Display the capture groups.
for (int ctr = 0; ctr < results.Count; ctr++)
Console.WriteLine("{0} at position {1}",
results[ctr], matchposition[ctr]);
// The example displays the following output:
// abc at position 3
// ab at position 3
// b at position 4
Dim matchposition As New List(Of Integer)
Dim results As New List(Of String)
' Define substrings abc, ab, b.
Dim r As New Regex("(a(b))c")
Dim m As Match = r.Match("abdabc")
Dim i As Integer = 0
While Not (m.Groups(i).Value = "")
' Add groups to string array.
results.Add(m.Groups(i).Value)
' Record character position.
matchposition.Add(m.Groups(i).Index)
i += 1
End While
' Display the capture groups.
For ctr As Integer = 0 to results.Count - 1
Console.WriteLine("{0} at position {1}", _
results(ctr), matchposition(ctr))
Next
' The example displays the following output:
' abc at position 3
' ab at position 3
' b at position 4
下面的示例使用命名的分组构造,从包含 DATANAME:VALUE
格式的数据的字符串中捕获子字符串,正则表达式通过冒号 (:) 拆分数据。
Regex r = new Regex("^(?<name>\\w+):(?<value>\\w+)");
Match m = r.Match("Section1:119900");
Console.WriteLine(m.Groups["name"].Value);
Console.WriteLine(m.Groups["value"].Value);
// The example displays the following output:
// Section1
// 119900
Dim r As New Regex("^(?<name>\w+):(?<value>\w+)")
Dim m As Match = r.Match("Section1:119900")
Console.WriteLine(m.Groups("name").Value)
Console.WriteLine(m.Groups("value").Value)
' The example displays the following output:
' Section1
' 119900
正则表达式模式 ^(?<name>\w+):(?<value>\w+)
的定义如下表所示。
模式 | 说明 |
---|---|
^ | 从输入字符串的开头部分开始匹配。 |
(?<name>\w+) | 匹配一个或多个单词字符。 此捕获组的名称为 name 。 |
: | 匹配冒号。 |
(?<value>\w+) | 匹配一个或多个单词字符。 此捕获组的名称为 value 。 |
Group 类的属性提供有关捕获的组的信息: Group.Value
属性包含捕获的子字符串, Group.Index
属性指示输入文本中捕获的组的起始位置, Group.Length
属性包含捕获的文本的长度, Group.Success
属性指示子字符串是否与捕获组所定义的模式匹配。
将限定符应用于组 (有关详细信息,请参阅 限定符 ) 修改一个捕获组两种方式捕获的关系:
- 如果对组应用
*
或*?
限定符(将指定零个或多个匹配项),则捕获组在输入字符串中可能没有匹配项。 在没有捕获的文本时,将如下表所示设置 Group 对象的属性。组属性 值 Success
false
Value
String.Empty Length
0 下面的示例进行了这方面的演示。 在正则表达式模式
aaa(bbb)*ccc
中,可以匹配第一个捕获组(子字符串bbb
)零次或多次。 由于输入字符串aaaccc
与此模式匹配,因此该捕获组没有匹配项。using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = "aaa(bbb)*ccc"; string input = "aaaccc"; Match match = Regex.Match(input, pattern); Console.WriteLine("Match value: {0}", match.Value); if (match.Groups[1].Success) Console.WriteLine("Group 1 value: {0}", match.Groups[1].Value); else Console.WriteLine("The first capturing group has no match."); } } // The example displays the following output: // Match value: aaaccc // The first capturing group has no match.
Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "aaa(bbb)*ccc" Dim input As String = "aaaccc" Dim match As Match = Regex.Match(input, pattern) Console.WriteLine("Match value: {0}", match.Value) If match.Groups(1).Success Then Console.WriteLine("Group 1 value: {0}", match.Groups(1).Value) Else Console.WriteLine("The first capturing group has no match.") End If End Sub End Module ' The example displays the following output: ' Match value: aaaccc ' The first capturing group has no match.
- 限定符可以匹配由捕获组定义的模式的多个匹配项。 在此情况下,
Value
对象的Length
和 Group 属性仅包含有关最后捕获的子字符串的信息。 例如,下面的正则表达式匹配以句点结束的单个句子。 此表达式使用两个分组构造:第一个分组构造捕获各个单词以及空白字符;第二个分组构造捕获各个单词。 如示例中的输出所示,虽然正则表达式成功捕获整个句子,但第二个捕获组仅捕获了最后一个单词。using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"\b((\w+)\s?)+\."; string input = "This is a sentence. This is another sentence."; Match match = Regex.Match(input, pattern); if (match.Success) { Console.WriteLine("Match: " + match.Value); Console.WriteLine("Group 2: " + match.Groups[2].Value); } } } // The example displays the following output: // Match: This is a sentence. // Group 2: sentence
Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "\b((\w+)\s?)+\." Dim input As String = "This is a sentence. This is another sentence." Dim match As Match = Regex.Match(input, pattern) If match.Success Then Console.WriteLine("Match: " + match.Value) Console.WriteLine("Group 2: " + match.Groups(2).Value) End If End Sub End Module ' The example displays the following output: ' Match: This is a sentence. ' Group 2: sentence
捕获集合
Group 对象仅包含有关最后一个捕获的信息。 但仍可从 CaptureCollection 属性返回的 Group.Captures 对象中获取由捕获组生成的整个捕获集。 集合中的每个成员均为一个表示由该捕获组生成的捕获的 Capture 对象,这些对象按被捕获的顺序排列(因而也就是遵循在输入字符串中按从左至右匹配捕获的字符串的顺序)。 可以通过以下两种方式之一来检索集合中的各个 Capture 对象:
- 通过使用构造循环访问集合,如
foreach
构造(在 C# 中)或For``Each
构造(在 Visual Basic 中)。 - 通过使用 CaptureCollection.Item[Int32] 属性按索引检索特定对象。 Item[Int32] 属性是 CaptureCollection 对象的默认属性(在 Visual Basic 中)或索引器(在 C# 中)。
如果未对捕获组应用限定符,则 CaptureCollection 对象将包含一个 Capture 对象,但该对象的作用不大,因为它提供的是有关与其 Group 对象相同的匹配项的信息。 如果对一个捕获组应用限定符,则 CaptureCollection 对象将包含该捕获组所生成的所有捕获,并且集合的最后一个成员将表示与 Group 对象相同的捕获。
例如,如果使用正则表达式模式 ((a(b))c)+
(其中,+ 限定符指定一个或多个匹配项)捕获字符串 abcabcabc
中的匹配项,则每个 CaptureCollection 对象的 Group 对象都将包含三个成员。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = "((a(b))c)+";
string input = "abcabcabc";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
Console.WriteLine("Match: '{0}' at position {1}",
match.Value, match.Index);
GroupCollection groups = match.Groups;
for (int ctr = 0; ctr < groups.Count; ctr++) {
Console.WriteLine(" Group {0}: '{1}' at position {2}",
ctr, groups[ctr].Value, groups[ctr].Index);
CaptureCollection captures = groups[ctr].Captures;
for (int ctr2 = 0; ctr2 < captures.Count; ctr2++) {
Console.WriteLine(" Capture {0}: '{1}' at position {2}",
ctr2, captures[ctr2].Value, captures[ctr2].Index);
}
}
}
}
}
// The example displays the following output:
// Match: 'abcabcabc' at position 0
// Group 0: 'abcabcabc' at position 0
// Capture 0: 'abcabcabc' at position 0
// Group 1: 'abc' at position 6
// Capture 0: 'abc' at position 0
// Capture 1: 'abc' at position 3
// Capture 2: 'abc' at position 6
// Group 2: 'ab' at position 6
// Capture 0: 'ab' at position 0
// Capture 1: 'ab' at position 3
// Capture 2: 'ab' at position 6
// Group 3: 'b' at position 7
// Capture 0: 'b' at position 1
// Capture 1: 'b' at position 4
// Capture 2: 'b' at position 7
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "((a(b))c)+"
Dim input As STring = "abcabcabc"
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then
Console.WriteLine("Match: '{0}' at position {1}", _
match.Value, match.Index)
Dim groups As GroupCollection = match.Groups
For ctr As Integer = 0 To groups.Count - 1
Console.WriteLine(" Group {0}: '{1}' at position {2}", _
ctr, groups(ctr).Value, groups(ctr).Index)
Dim captures As CaptureCollection = groups(ctr).Captures
For ctr2 As Integer = 0 To captures.Count - 1
Console.WriteLine(" Capture {0}: '{1}' at position {2}", _
ctr2, captures(ctr2).Value, captures(ctr2).Index)
Next
Next
End If
End Sub
End Module
' The example dosplays the following output:
' Match: 'abcabcabc' at position 0
' Group 0: 'abcabcabc' at position 0
' Capture 0: 'abcabcabc' at position 0
' Group 1: 'abc' at position 6
' Capture 0: 'abc' at position 0
' Capture 1: 'abc' at position 3
' Capture 2: 'abc' at position 6
' Group 2: 'ab' at position 6
' Capture 0: 'ab' at position 0
' Capture 1: 'ab' at position 3
' Capture 2: 'ab' at position 6
' Group 3: 'b' at position 7
' Capture 0: 'b' at position 1
' Capture 1: 'b' at position 4
' Capture 2: 'b' at position 7
下面的示例使用正则表达式 (Abc)+
来在字符串 XYZAbcAbcAbcXYZAbcAb
中查找字符串 Abc
的一个或多个连续匹配项。 该示例演示了使用 Group.Captures 属性来返回多组捕获的子字符串。
int counter;
Match m;
CaptureCollection cc;
GroupCollection gc;
// Look for groupings of "Abc".
Regex r = new Regex("(Abc)+");
// Define the string to search.
m = r.Match("XYZAbcAbcAbcXYZAbcAb");
gc = m.Groups;
// Display the number of groups.
Console.WriteLine("Captured groups = " + gc.Count.ToString());
// Loop through each group.
for (int i=0; i < gc.Count; i++)
{
cc = gc[i].Captures;
counter = cc.Count;
// Display the number of captures in this group.
Console.WriteLine("Captures count = " + counter.ToString());
// Loop through each capture in the group.
for (int ii = 0; ii < counter; ii++)
{
// Display the capture and its position.
Console.WriteLine(cc[ii] + " Starts at character " +
cc[ii].Index);
}
}
}
// The example displays the following output:
// Captured groups = 2
// Captures count = 1
// AbcAbcAbc Starts at character 3
// Captures count = 3
// Abc Starts at character 3
// Abc Starts at character 6
// Abc Starts at character 9
Dim counter As Integer
Dim m As Match
Dim cc As CaptureCollection
Dim gc As GroupCollection
' Look for groupings of "Abc".
Dim r As New Regex("(Abc)+")
' Define the string to search.
m = r.Match("XYZAbcAbcAbcXYZAbcAb")
gc = m.Groups
' Display the number of groups.
Console.WriteLine("Captured groups = " & gc.Count.ToString())
' Loop through each group.
Dim i, ii As Integer
For i = 0 To gc.Count - 1
cc = gc(i).Captures
counter = cc.Count
' Display the number of captures in this group.
Console.WriteLine("Captures count = " & counter.ToString())
' Loop through each capture in the group.
For ii = 0 To counter - 1
' Display the capture and its position.
Console.WriteLine(cc(ii).ToString() _
& " Starts at character " & cc(ii).Index.ToString())
Next ii
Next i
' The example displays the following output:
' Captured groups = 2
' Captures count = 1
' AbcAbcAbc Starts at character 3
' Captures count = 3
' Abc Starts at character 3
' Abc Starts at character 6
' Abc Starts at character 9
单个捕获
Capture 类包含来自单个子表达式捕获的结果。 Capture.Value 属性包含匹配的文本,而 Capture.Index 属性指示匹配的子字符串在输入字符串中的起始位置(从零开始)。
下面的示例分析针对选定城市的温度的输入字符串。 逗号( ,
)用于将城市与其温度分隔开,而分号( ;
)用于将每个城市的数据分隔开。 整个输入字符串表示一个匹配项。 在用于分析字符串的正则表达式模式 ((\w+(\s\w+)*),(\d+);)+
中,城市名称将分配给第二个捕获组,而温度将分配到第四个捕获组。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "Miami,78;Chicago,62;New York,67;San Francisco,59;Seattle,58;";
string pattern = @"((\w+(\s\w+)*),(\d+);)+";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
Console.WriteLine("Current temperatures:");
for (int ctr = 0; ctr < match.Groups[2].Captures.Count; ctr++)
Console.WriteLine("{0,-20} {1,3}", match.Groups[2].Captures[ctr].Value,
match.Groups[4].Captures[ctr].Value);
}
}
}
// The example displays the following output:
// Current temperatures:
// Miami 78
// Chicago 62
// New York 67
// San Francisco 59
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "Miami,78;Chicago,62;New York,67;San Francisco,59;Seattle,58;"
Dim pattern As String = "((\w+(\s\w+)*),(\d+);)+"
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then
Console.WriteLine("Current temperatures:")
For ctr As Integer = 0 To match.Groups(2).Captures.Count - 1
Console.WriteLine("{0,-20} {1,3}", match.Groups(2).Captures(ctr).Value, _
match.Groups(4).Captures(ctr).Value)
Next
End If
End Sub
End Module
' The example displays the following output:
' Current temperatures:
' Miami 78
' Chicago 62
' New York 67
' San Francisco 59
该正则表达式的定义如下表所示。
模式 | 描述 |
---|---|
\w+ | 匹配一个或多个单词字符。 |
(\s\w+)* | 匹配零个或多个以下模式:一个空白字符后跟一个或多个单词字符。 此模式匹配包含多个单词的城市名称。 这是第三个捕获组。 |
(\w+(\s\w+)*) | 匹配以下模式:一个或多个单词字符,后跟零个或多个一个空白字符与一个或多个单词字符的组合。 这是第二个捕获组。 |
, | 匹配逗号。 |
(\d+) | 匹配一个或多个数字。 这是第四个捕获组。 |
; | 匹配分号。 |
((\w+(\s\w+)*),(\d+);)+ | 匹配一个或多个以下模式:一个单词后跟任何其他单词,后跟一个逗号、一个或多个数字和一个分号。 这是第一个捕获组。 |
另请参阅
System.Text.RegularExpressions
.NET 正则表达式
正则表达式语言 - 快速参考
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论