Regular Expressions (3) ---- Using Regex in .net
Before you look at this article, assume you already understand some of the knowledge about regular expressions and know how to apply it. This article only describes how to use regular expressions in .NET.
Finally, a small program that applies regular expressions to VB program syntax.
Name space and related classes
All and regular expression related classes in .NET are defined under System.Text.RegularExpressions namespace.
They are:
Class
Description
Regex
Regular expressions of non-variable (read-only), including some static methods
Capture
Contains the result of a single sub-expression match, cannot be instantiated, and must be accessed via Group and Match.
CaptureCollection
Capture sequence
Group
A set of expressions matching the result, inheriting by capture
GROUPCOLLECTION
GROUP sequence
Match
A matched result of a regular expression, inheriting by group
Matchcollection
Match sequence
The relationship between the various classes is illustrated by the drawings and examples below:
example:
As shown above:
The matching string is: "XGRaygreyxgreyxgray"
Regular expressions are: (GR [AE]) Y
Indicates that "Gray" or "Grey" is one or n times.
It can be seen from the generated matching tree:
There have a total of three matching. Each matching value is: GrayGray, Gray, Grey.
Take the first match as an example:
Matching a CCP includes two groups: The values of each group are: GrayGrey, Grey.
The first group has only one capture result, which is GrayGrey.
The second group has two capture results, which are GRAY and GREY, respectively.
The detailed role of these classes is explained below:
Match:
Match represents a matching operation of a regular expression.
As in the above example:
(GR [AE] Y) Get three match results. Graygrey, Gray, Grey, respectively.
This is equal to the regular expression interpreter to remove (GR [AE] Y) (GR [AE] Y) , (Gray) and (Grey) three separate sub-expressions.
Each sub-expression corresponds to a match result.
Regex
Result
(GR [AE] Y)
Graygrey
(Gray)
Gray
(GREY)
Grey
Matchcollection
MatchCollection is represented by all the characters of the Match results.
You can access Match results of each sub-expression by traversing the collection of matchs.
For Each M As Match in Regex.matches ("GrayGrey", (GR [AE] Y) ")
'Use match result here.
NEXT
Group:
Each child is default a group. So the results of all groups of child expressions are stored in the first group, which is why each Match's result is the same as the result of the first group, because they are one.
In .NET, the sub-expression enclosed in parentheses indicates that this is a group.
Group named can be displayed by ("
such as:
Regex r = new regex ("(?
ABC) ")
. A group is defined, the name is G1.
In the program, we can access the matching results of the group by grouping.
DIM M as match = r.match ("xxabcabc") DIM G1 AS Group = M.Groups ("G1")
GROUPCOLLECTION
GroupCollection represents a collection of Groups in a match.
You can access a group by grouping or claim.
CATPure:
Saved in Capture is the minimum match result of each sub-expression, which is equivalent to an atom match, such as a match A, then the result A is saved in Capture, the result of Group is just a combination of the Capture result.
So if a match is only a group, there is only one capture in this group, and the result of this Capture is the result of the entire MATCH.
CaptureCollection
Capture's sequence.
Vbhightdemo
Let's introduce this application that applies regular expressions to the VB syntax.
The principle of this program is based on the regular expression, and the color changes of the corresponding characters are changed according to the results of the match.
The key place is to get the regular expression of the formation.
PRIVATE VBIMPORTS As Regex = new regex ("(?
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 = "(?
& "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 |
& "While | WitHevents) {1} [^ / W]"
Private vbkeywords as regex = new regex (aa, regexoptions.compiled)
Private systemkeywords as regex = new regex ("(Application | Console | Environment | GC | ThreadPool | Math) {1}", regexoptions.compiled
Private Brackets as regex = new regex ("(/ {| / [| /)", regexoptions.compiled)
When matching, you can give the specified matching option, such as ignore the uppercase, or multi-line.
Then traverse and highlight the result.
For Each M as match in regex.matches (s)
Me.colorstring (Start M.Groups (0) .index, M.Groups (0) .length, color)
NEXT
Because the group saved is the result of all Capture's combination, only the strings in Groups (0) are enough, and no longer deduplicate the value of Captures in Group.
Please refer to it will be correct.
:)
If The Source Code Needed, Plas Mail To Me: Etmonitor@msn.com.