Regular Expressions (3) ---- Using Regex in .NET

** Regular Expressions (3) ---- Using Regex in .NET **


看这篇文章之前,假定你已经了解了有关正则表达式的一些知识,并且知道如何去应用它。本篇文章只介绍如何在 .NET 中使用正则表达式。

最后是一个应用正则表达式的给 VB 程序语法加亮的小程序。


** 名称空间和相关的类 ** ** **

.NET 中所有和正则表达式相关的类都定义在 System.Text.RegularExpressions 名称空间下。

分别是:

Class

|

Description

---|---

Regex

|

不可变的 ( 只读 ) 的正则表达式的实例,包括一些静态方法

Capture

|

包含单个子表达式匹配的结果,不能实例化,必须通过 Group 和 Match 来访问。

CaptureCollection

|

Capture 的序列

Group

|

一组表达式匹配的结果,由 Capture 继承而来

GroupCollection

|

Group 的序列

Match

|

一个正则表达式的匹配结果,由 Group 继承而来

MatchCollection

|

Match 的序列

** 下面用图和例子说明各个类之间的关系: ** ** **

** 例: **

** ** ** **


如上图:

匹配的字符串是: ” xgraygreyxgreyxgray ”

正则表达式是: ( gr [ ae ])y

表示匹配 ”gray” 或者 ”grey” 一次或 n 次。

从生成的匹配树可以看到:

总共找到了三个匹配。每个匹配的值分别为: graygray , gray , grey 。

以第一个匹配为例:

匹配一中共包括两个组:每个组的值分别为: graygrey , grey 。

第一个组只有一个捕获结果,就是 graygrey 。

第二组有两个捕获结果,分别为 gray 和 grey 。

下面说明这些类的详细作用:

** Match ** ** : ** ** **

Match 表示的是一个正则表达式的匹配操作。

如上例:

( Gr [ ae ]y)+ 得到三个 Match 结果。分别是 graygrey , gray , grey 。

这等于是正则表达式解释器把 ( gr [ ae ]y)+ 吃拆成了 ( gr [ ae ]y)+ , (gray)+ 和 (grey)+ 三个单独的子表达式。

每个子表达式对应一个 Match 结果。

Regex

|

Result

---|---

( gr [ ae ]y)+

|

Graygrey

(gray)+

|

Gray

(grey)+

|

Grey

** MatchCollection ** ** **

MatchCollection 表示的就是所有的字表达式的 Match 结果的集合。

可以通过遍历 Matchs 的集合来访问各个子表达式的 Match 结果。

For each m as Match in Regex.Matches ( “ graygrey ”, ( gr [ ae ]y)+”)

‘ Use match result here.

Next

** Group ** ** : ** ** **

每个子表达式默认就是一个组。所以子表达式的所有组的结果都保存在第一个组中,这也是为什么每个 Match 的结果和第一个 Group 的结果是一样的,因为它们就是一个。

在 .NET 中,用括号括起来的子表达式表示这是一个组。

可以通过 (?< groupName >< 子表达式 >) 来显示的给组命名。

比如:

Regex r = new Regex ( “(? abc )+”) 。就定义了一个组,名字为 g1 。

在程序中,我们可以通过组命来访问组的匹配结果。

Dim m As Match = r.Match ( " xxabcabc ")

Dim g1 As Group = m.Groups ( “g1” )

** GroupCollection ** ** **

GroupCollection 表示一个 Match 中 Group 的集合。

可以通过组命或者索引来访问一个组。

** Catpure ** ** : ** ** **

Capture 中保存的是每一个子表达式的最小匹配结果,它相当于是原子匹配,比如 a 匹配 a ,则结果 a 就保存在 capture 中, group 的结果只是所的 capture 结果的组合。

所以如果一个匹配只有一个 group ,这个 group 中又只有一个 capture ,则这个 capture 的结果就是整个 Match 的结果。

** CaptureCollection ** ** **

Capture 的序列。

** VBHightDemo ** ** **

下面介绍一下这个应用正则表达式给 VB 语法加亮的程序。

这个程序的原理就是根据正则表达式分析每一行字符串,根据匹配结果,把相应的字符的颜色改变。

关键的地方就是拿到组建的正则表达式。

Private VBImports As Regex = New Regex ( "(?<![\.\w])(imports)[^\w]", RegexOptions.Compiled )

Private QuotedString As Regex = New Regex ( """" & "[^""]*" & """", RegexOptions.Compiled )

Private VBComment As Regex = New Regex ( "'.*")

Private EmptyLine As Regex = New Regex ( "^\\s*\\Z", RegexOptions.Compiled )

Private VBRegion As Regex = New Regex ( "(# region|#end region){1}", RegexOptions.Compiled )

'-- use lookbehind , keywords can not follow by a '.' or a characotr

Dim aa As String = "( ?< ![\.\w])( as|boolean|break|byte|byval|byref |" _

& " case|catch|char|checked|class|const|continue |" _

& " decimal|dim|default|delegate|double|else |" _

& " enum|end|event|exit|explicit|extern|false|each |" _

& " float|friend|for|goto|if|implicit|inherits |" _

& " integer|interface|internal|is|lock|long |" _

& " namespace|new|next|overrides|overloads|object|operator|out |" _

& " override|params|private|protected |" _

& " public|readonly|ref|return|sbyte|sealed |" _

& " short|static|string|sub |" _

& " structure|switch|me|mybase|throw|true|try|typeof |" _

& " uint|ulong|unchecked|ushort|using |" _

Published At
Categories with Web编程
comments powered by Disqus