惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
S
Schneier on Security
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Full Disclosure
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
Hacker News: Ask HN
Hacker News: Ask HN
G
Google Developers Blog
H
Heimdal Security Blog
O
OpenAI News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LangChain Blog
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
IT之家
IT之家
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Know Your Adversary
Know Your Adversary
博客园 - 聂微东
The Cloudflare Blog
C
Check Point Blog
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Tor Project blog
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
小众软件
小众软件
Cloudbric
Cloudbric
A
Arctic Wolf

博客园 - 无极.net

.NET正则表达式使用高级技巧之工作特点 .NET正则表达式使用高级技巧之反向引用 - 无极.net - 博客园 .NET正则表达式使用高级技巧之替换类 C#中利用正则表达式实现字符串搜索 字符串长度,同时考虑二个英文=一个中文 - 无极.net - 博客园 c# 添加图片水印,可以指定水印位置+生成缩略图 - 无极.net - 博客园 FLASH图片新闻代码 - 无极.net - 博客园 正则表达式30分钟入门教程 v2.1 两个固定宽度的DIV,不换行 - 无极.net - 博客园 UrlReWriter 使用经验小结 如何在JS里取得两个数相除的整数和余数,那么如何判断一个数是整数呢? 向上滚动 flash从asp中调用变量 菜单 ASP.NET AJAX(开发代号Atlas)重要参考资源大收集 索引[]在字符串中的用法 div 相对于浮动层定位,不占位 储存过程里设置了OUTPUT,取值 SQL 声明变量 declare
.NET正则表达式使用高级技巧之组的概念 - 无极.net - 博客园
无极.net · 2007-07-23 · via 博客园 - 无极.net

正则表达式中的组是很重要的一个概念,它是我们通向高级正则应用的的桥梁。

  组的概念

  一个正则表达式匹配结果可以分成多个部分,这就是组(Group)的目的。能够灵活的使用组后,你会发现Regex真是很方便,也很强大。

  先举个例子

public static void Main()
{
 string s = "2005-2-21";
 Regex reg = new Regex(@"(?<y>\d{4})-(?<m>\d{1,2})-(?<d>\d{1,2})",RegexOptions.Compiled);
 Match match = reg.Match(s);
 int year = int.Parse(match.Groups["y"].Value);
 int month = int.Parse(match.Groups["m"].Value);
 int day = int .Parse(match.Groups["d"].Value);
 DateTime time = new DateTime(year,month,day);
 Console.WriteLine(time);
 Console.ReadLine();
}

  以上的例子通过组来实现分析一个字符串,并把其转化为一个DateTime实例,当然,这个功能用DateTime.Parse方法就能很方便的实现。

  在这个例子中,我把一次Match结果用(?<name>)的方式分成三个组"y","m","d"分别代表年、月、日。

  现在我们已经有了组的概念了,再来看如何分组,很简单的,除了上在的办法,我们可以用一对括号就定义出一个组,比如上例可以改成:

public static void Main()
{
 string s = "2005-2-21";
 Regex reg = new Regex(@"(\d{4})-(\d{1,2})-(\d{1,2})",RegexOptions.Compiled);
 Match match = reg.Match(s);
 int year = int.Parse(match.Groups[1].Value);
 int month = int.Parse(match.Groups[2].Value);
 int day = int .Parse(match.Groups[3].Value);
 DateTime time = new DateTime(year,month,day);
 Console.WriteLine(time);
 Console.ReadLine();
}

  从上例可以看出,第一个括号对包涵的组被自动编号为1,后面的括号依次编号为2、3……

public static void Main()
{
 string s = "2005-2-21";
 Regex reg = new Regex(@"(?<2>\d{4})-(?<1>\d{1,2})-(?<3>\d{1,2})",RegexOptions.Compiled);
 Match match = reg.Match(s);
 int year = int.Parse(match.Groups[2].Value);
 int month = int.Parse(match.Groups[1].Value);
 int day = int .Parse(match.Groups[3].Value);
 DateTime time = new DateTime(year,month,day);
 Console.WriteLine(time);
 Console.ReadLine();
}

  再看上例,我们用(?<数字>)的方式手工给每个括号对的组编号,(注意我定义1和2的位置时不是从左到右定义的)

  通过以上三例,我们知道了给Regex定义Group的三种办法以及相应的引用组匹配结果的方式。

  然后,关于组定义,还有两点请注意:

  1、因为括号用于定义组了,所以如果要匹配"("和")",请使用"\("和"\)"(关于所有特殊字符的定义,请查看相关Regex expression帮助文档)。

  2、如果定义Regex时,使用了ExplicitCapture选项,则第二个例子不会成功,因为此选项要求显式定义了编号或名字的组才捕获并保存结果,如果你没有定义ExplicitCapture选项,而有时又定义了类式于(A|B)这样的部分在表达式,而这个(A|B)你又并不想捕获结果,那么可以使用“不捕获的组”语法,即定义成(?:)的方式,针对于(A|B),你可以这样来定义以达到不捕获并保存它到Group集合中的目的--(?:A|B)。