C # Regex in-depth Regular Expressions

Source: Internet
Author: User

Regular expressions is a set of syntax matching rules. Various languages, such as Perl,. Net, and Java, have their own shared Regular Expression Libraries. In. Net, this class library is called Regex.
To put it simply, Regex is an application class used to find matching strings from the character window. With Regex, programmers can easily extract the data information they need from a piece of data. Here is a simple example to give you a general understanding of Regex:
Regex regex = new Regex (@ "d + ");
Match m = regex. Match ("fox9212 gold ");
Console. WriteLine (m. Value. ToString ());
The result is obvious. regex finds the numeric string in the string "fox 9212gold" and the output result is "9212 ".
After having a basic concept for Regex, I need to tell you a very good news that Regex can do more than that for us, it is a set of powerful syntax matching rules. Of course, there is still a bad message waiting for us, that is, the powerful syntax rules naturally require a large number of complicated keyword support, which also brings great difficulties to Regex learning. To really grasp the regular expression, not a few samples can be fully revealed and explained.

Create a Regex object
There are three Regex constructor types. We will not discuss the default constructor here. In the other two constructors, one constructor receives the regular expression string as the input parameter, and the other uses the regular expression string and RegexOptions as the input parameter. For example:
 
Regex regex = new Regex ("w + $ ");
Regex regex = new Regex ("s +", RegexOptions. IgnoreCase | RegexOptions. Multiline );
RegexOptions can provide some special help for us. For example, IgnoreCase can ignore case sensitivity during matching, and Multiline can adjust the meaning of ^ and $ to match the beginning and end of a row.
We constructed a regular expression above, but we didn't use it to do anything. Now we can use the following methods to perform operations on string objects.
Match string
Regex has two matching methods: Match () and Matches (), which respectively indicate matching one and matching multiple results. Matches is used to show how to use Regex to obtain and display the matching string.
 
Public static void showMatches (string expression, RegexOptions option, string MS)
{
Regex regex = new Regex (expression, option );
MatchCollection matches = regex. Matches (MS );
// Show matches
Console. writeLine ("////////////////----------------------------------////////////////");
Console. WriteLine ("string:" {0} "expression:" {1} "match result is:", MS, expression );
Foreach (Match m in matches)
{Console. WriteLine ("match string is:" {0} ", length: {1}", m. Value. ToString (), m. Value. Length );
}
Console. WriteLine ("matched count: {0}", matches. Count );
}
The Matched method compares the input parameter strings and regular expressions to find all the matching results and transmits the results as MatchCollection. In this way, you can quickly obtain all the results by simply traversing the collection.

Group concept

When you get such a string "the final score is: 19/24", you certainly want to have a regular expression that not only can find a string like data1/data2, data1 and data2 should also be directly transmitted as separate results. Otherwise, you need to analyze the string in the form of "19/24" to obtain the score of both parties. Obviously, regular expressions do not ignore this issue, so it adds the group concept. You can put the search results into different groups respectively, and obtain the results of these groups by group name or group. For example, in the above example, we can use @ "(d +)/(d +)" as the expression. Let's take a look at the results:
 
Regex regex = new Regex (@ "(d +)/(d + )");
MatchCollection matches = regex. Matches (@ "the last score is: 19/24 ");
// Show matches
Console. writeLine ("////////////////----------------------------------////////////////");
Foreach (Match m in matches)
{
// Console. writeLine ("match string is:" {0} ", length: {1}", // m. value. toString (), m. value. length );
Foreach (string name in regex. GetGroupNames ())
{
Console. WriteLine ("capture group" {0} "value is:" {1} "", name, m. Groups [name]. Value );
}
}
Console. WriteLine ("matched count: {0}", matches. Count );
Output:
////////////////----------------------------------////////////////
Capture group "0" value is: "19/24"
Capture group "1" value is: "19"
Capture group "2" value is: "24"
Matched count: 1

Now it is clear that the Regex object puts the matching results into group 0. At the same time, the matched group information is also placed in the corresponding group. By default, the group name is an integer that increases from 1. 0 is the reserved name, which is used to indicate the entire string to be matched. Since there is a "default situation" concept, it naturally means that you can customize the group name. The method is very simple. Add the following before the Group :? <Name>. Now, change the regular expression @"(? <Score1> d + )/(? <Score1> d +) ", now let's look at the result:
////////////////----------------------------------////////////////
Capture group "0" value is: "19/24"
Capture group "score1" value is: "19"
Capture group "score2" value is: "24"
Matched count: 1

Change it to your own name, haha! To make it easier to see all the results in future tests, we have made a small adjustment to the showMatches () We have discussed earlier. In this way, if the expression contains the Group definition, we can also directly view all the group information without modifying any code. The adjusted method showMatchesPro () as follows:

Public static void showMatchesPro (string expression, RegexOptions option, string MS)
{
Regex regex = new Regex (expression, option );
MatchCollection matches = regex. Matches (MS );
// Show matches
Console. writeLine ("////////////////----------------------------------////////////////");
Console. WriteLine ("string:" {0} "expression:" {1} "match result is:", MS, expression );
Foreach (Match m in matches)
{
Foreach (string name in regex. GetGroupNames ())
{
Console. WriteLine ("capture group" {0} "value is:" {1} "", name, m. Groups [name]. Value );
}
}
Console. WriteLine ("matched count: {0}", matches. Count );
// Show group name
Console. WriteLine ("group name count {0}", regex. GetGroupNames (). Length );
Foreach (string name in regex. GetGroupNames ())
{
Console. WriteLine ("group name:" {0} "", name );
}
}
Replace string
Regex also provides a convenient matching result replacement function. To facilitate the test, we also write the method as follows:

Public static string replaceMatch (string expression, RegexOptions option, string MS, string rep)
{
Regex regex = new Regex (expression, option );
String result = regex. Replace (MS, rep );
Console. writeLine ("////////////////----------------------------------////////////////");
Console. WriteLine ("string:" {0} ", expression:" {1} ", replace by:" {2} "", MS, expression, rep );
Console. WriteLine ("replace result string is:" {0} ", length: {1}", result. ToString (), result. Length );
Return result;
}

Regex. Replace generally accepts two strings as input parameters. The first string is the input string. The second string is used to replace the matching string. It can contain special strings to represent special conversions.

Special string replacement result
$ & Matched string, or $0
$1, $2,... match the corresponding group in the string and use the index
$ {Name} matches the corresponding group in the string with the name
$ 'Match the string before the position
$ 'String after matching position
$ A '$' character
$ _ Input string
$ + Data in the last group that matches the string

Have you read so many strange special strings? Well, let's get two samples to see the results!
Sample1:
ReplaceMatch (@ "d +", RegexOptions. None, "fef 12/21 df 33/14 727/1", "<$ &> ");
Output, All numeric data is replaced with <data>:
////////////////----------------------------------////////////////
String: "fef 12/

Related Article

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.