Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 25 章 正则表达式 正则表达式

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

正则表达式语言可识别字符模式。支持正则表达式的 .NET 类型基于 Perl 5 正则表达式,同时支持搜索和搜索/替换功能。

正则表达式用于执行以下任务

  • 验证密码和电话号码等文本输入

  • 将文本数据解析为更结构化的形式(如 NuGet 版本字符串)

  • 替换文档中的文本模式(如只替换整个单词)

本章分为概念部分和参考部分,概念部分教授.NET中正则表达式的基础知识,参考部分介绍正则表达式语言

System.Text.RegularExpressions 中定义了所有正则表达式类型。

备注

本章中的示例已全部预加载到 LINQPad 中,其中还包括一个交互式 RegEx 工具(按 Ctrl+Shift+F1 键)。在线工具可在http://regexstorm.net/tester 上获取。

正则表达式基础知识

最 常见的正则表达式运算符之一是定量符? ,定量符表示 0 或 1 次匹配前一项。换句话说,? 意味着可选。项可以是单个字符,也可以是方括号中的复杂字符结构。例如,正则表达式"colou?r" 匹配colorcolour,但不匹配colouur

Console.WriteLine (Regex.Match ("color",   @"colou?r").Success);  // True
Console.WriteLine (Regex.Match ("colour",  @"colou?r").Success);  // True
Console.WriteLine (Regex.Match ("colouur", @"colou?r").Success);  // False

Regex.Match 在更大的字符串中进行搜索。它返回的对象具有匹配的 和 以及实际匹配的 的属性:Index Length Value

Match m = Regex.Match ("any colour you like", @"colou?r");

Console.WriteLine (m.Success);     // True
Console.WriteLine (m.Index);       // 4
Console.WriteLine (m.Length);      // 6
Console.WriteLine (m.Value);       // colour
Console.WriteLine (m.ToString());  // colour

可以将Regex.Match 视为stringIndexOf 方法的更强大版本。区别在于它搜索的是模式而不是字面字符串。

IsMatch 方法是调用Match 然后测试Success 属性的快捷方式。

正则表达式引擎默认从左到右工作,因此只返回最左边的匹配项。您可以使用NextMatch 方法返回更多匹配结果:

Match m1 = Regex.Match ("One color? There are two colours in my head!",
                        @"colou?rs?");
Match m2 = m1.NextMatch();
Console.WriteLine (m1);         // color
Console.WriteLine (m2);         // colours

Matches 方法以数组形式返回所有匹配结果。我们可以重写前面的示例如下:

 foreach ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038