Use regular expressions to view. NET Programming-. NET core regular expressions

Source: Internet
Author: User
Tags expression engine

. NET core regular expressions

 

Thank you for your support in the previous article. I am very glad that my article will bring you some benefits. Thank you.

. Today's explanation is divided into the following parts:

1. Overview of core objects

2. Detailed explanation of core objects

 

First, let's take a look at the first part: 1. Core object Overview

In. in NET, the regular expression function is provided through the high interaction of 7 classes, but we only need to master three of them to complete most of the functions, so let's take a look at these three classes.

1.1Regex object:

First, we create a Regex object:

 

Code
Regex regex = new Regex (@ "\ s + (\ d) $ ");

 

After creating an object, you can Match the character text. In this case, you need to call the Match method:

 

Code
Match match = regex. Macth ("May 16,2007 ");

 

Please pay attention to the returned type!

 

1.2Match object

The Match method of the Regex object is created and the Match object is returned to provide matching information. A Match object has many attributes, such as Success, indicating whether the Match is successful. It is of the bool type. there is also the Value attribute. If the matching is successful, the Value stores a copy of the actually matched text.

The details returned by the Match object also include the text matching the capturing brackets .. NET uses two methods to obtain the matched captured text: Use the Groups [I] of the Match object. value (I is a number ).

Use the Result method. These will be discussed later.

1.3Group object

We talked about some Groups knowledge before. As you can guess, Groups is a collection of Group objects, so Groups [1]. value is the first text Value to capture matching parentheses. each

Each Group capturing bracket has a corresponding Group object. there is also a "virtual group" with the ID 0, which stores global matching information. for example, in the above example, Groups [0]. the Value of Value is "May

16,2007 ", and the Value of Groups [1]. Value is" 16 ".

Note: the match in the following example is defined in the above example:

 

Therefore, match. value and match. groups [0]. value is equivalent-all copies of globally matched text. the first method is very concise, but we need to remember that there are 0 groups because of match. groups. count contains 0 groups, so the above example matches. groups. count is 2.

 

Note: When matching, all results are calculated. The regular expression is applied to the character text to obtain a Match object. All results, such as the matching position, are obtained, each captured group matches the content, and so on.

And encapsulate it in a Match object. We can access the properties and methods of the Match object to obtain our results.

 

2. Detailed explanation of core objects

After reading the above sections, we have a basic understanding of some common objects. In practice, we seldom explicitly declare Regex objects, that is, we generally do not create a new Regex object. because the Regex class has many static methods.

2.1 create a Regex object.

It is not difficult to create a Regex object because the Regex constructor is relatively simple and can accept a parameter (as a string of a regular expression). We have seen the previous example. or accept two parameters (a regular expression and an enumerated value ). the following are examples:

 

Constructor that accepts a parameter:

Code
Regex regex = new Regex (@ "\ s + (\ d) $ ");

 

Constructor that accepts two parameters:

Code
// Case-insensitive match and match multiple rows
Regex regex = new Regex (@ "^ \ s + (\ d) $", RegexOptions. IgnoreCase | RegexOptions. Multiline );

 

You may not be very clear about the enumerated values below (only common values ):

The value of RegexOptions indicates:

RegexOptions. IgnoreCase: Specifies a case-insensitive match;

RegexOptions. Multiline: indicates that the regular expression adopts the enhanced anchor mode during Matching. That is to say, it can match the linefeed inside the string, not only the knowledge to match the start and end of the entire string: for example:

A string, string str = "This is a apple. (line feed)

Hello xiaoyang. (line feed)

Hello everyone! "

(Note: You can even read an article into a string. There must be many lines in the article)

If we want to match all a in str, then you can use the expression Regex regex = new Regex (@ "^ a $");, then you can only match one row, that is, the matching result is a apple, followed by two lines.

No match, so you can use RegexOptions. Multiline to match multiple rows.

 

RegexOptions. Singleline: indicates that only one row is matched.

RegexOptions. explicitCapture: in regular expressions, brackets "()" are capturing parentheses, that is, You Can index the corresponding values in the results with Groups [I, if you use this enumeration

The parentheses "()" in your regular expression are not capturing parentheses. Then your parentheses are grouping. But note: the name capturing parentheses are not affected, that is (? <Name>...) can still be captured, you can access

Groups ["name"]. Value to obtain your matched text.

 

RegexOptions. RightToLeft: performs a match from right to left, while our expression is usually matched from left to right.

 

RegexOptions. Complied: Indicates whether your regular expression is to be compiled. It is similar to the relationship between the storage structure in our database and general query statements. If your regular expression is used only once

 

It makes no sense to compile. If it is very often used, after compilation, the matching will be faster.

 

The enumerated values above are very commonly used. If there are a few other values that are not very useful, don't mention them. in fact, we can use multiple enumerations in the constructor, for example, matching multiple rows at the same time without case sensitivity and compiling expressions. you

 

You can do this:

Code

// Case-insensitive matching and multi-row matching, compilation
Regex regex = new Regex (@ "^ \ s + (\ d) $", RegexOptions. IgnoreCase | RegexOptions. Multiline | RegexOptions. Complied );

 

Note: Use "|" to connect multiple tables.

The above describes how to create a Regex object. Next we will go on:

2.2 Use A Regex object:

First, create a Regex to facilitate the following explanation:

 

Code

// Case-insensitive matching and multi-row matching, compilation
Regex regex = new Regex (@ "^ \ s + (\ d) $", RegexOptions. IgnoreCase | RegexOptions. Multiline | RegexOptions. Complied );

 

We can use this regex object to match the text.

2.2.1IsMatch method:

This method uses two reloads: regex. IsMatch (strString), regex. IsMatch (strString, offSet)

The IsMatch method applies the target Regular Expression (^ \ s + (\ d) $ to the target string (strString) and returns the bool value, indicating whether the match is successful, while offSet is an integer, the position from the string.

Set to start matching.

2.2.2Match method:

This method has three reloads, which is very simple.

Regex. Match (strString ),

Regex. Match (strString, offSet)

Regex. Match (strString, offSet, maxlength );

Here, strString = the string to be matched, offSet = start position (int), maxlength = the length of the string to be matched, it may be a long string, you just want to match 10 characters.

The Match method returns a Match object. We can obtain the matching information in the returned object.

Note: If the maxlength parameter is provided, a special match is performed. The Regular Expression Engine Starts to calculate the characters starting from offSet. The Regular Expression Engine regards the text of maxlength as the entire target string, and

Assume that the characters out of this range do not exist, so "^" can only match the offSet position in the string, while "$" can match the position of maxlength ., even if your regular expression contains a loop view, the loop view cannot be "viewed ".

Characters out of this range.

2.2.3Matches method:

In fact, the Matches method returns a set of Match objects, which can be analogous to the relationship between Groups and Group mentioned above.

Example:

Code
Regex regex = new Regex (@ "\ w + ");
String str = "hello world ";
MatchCollection matchCollection = regex. Matchs (str );

 

There are two match objects in matchCollection, that is, matchCollection. Count is 2. You can iterate the collection object and use the Value attribute of Match to get matching: Helle and World.

 

Today we are here. The next article will talk about the slightly more advanced Regex objects!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.