C # system. Text. regularexpressions. RegEx (2 ).

Source: Internet
Author: User
Tags expression engine

(6) Matching of special characters

String x = "//";
RegEx R1 = new RegEx ("^ // $ ");
Console. writeline ("R1 match count:" + r1.matches (x). Count); // 1
RegEx r2 = new RegEx (@ "^ // $ ");
Console. writeline ("R2 match count:" + r2.matches (x). Count); // 1
RegEx R3 = new RegEx ("^ // $ ");
Console. writeline ("R3 match count:" + r3.matches (x). Count); // 0
// Match "/"

String x = "/"";
RegEx R1 = new RegEx ("^/" $ ");
Console. writeline ("R1 match count:" + r1.matches (x). Count); // 1
RegEx r2 = new RegEx (@ "^" "$ ");
Console. writeline ("R2 match count:" + r2.matches (x). Count); // 1
// Match double quotes

(7) group and non-capture group

String x = "Live for nothing, die for something ";
String y = "Live for nothing, die for somebody ";
RegEx r = new RegEx (@ "^ live ([A-Z] {3}) No ([A-Z] {5 }), die/1 Some/2 $ ");
Console. writeline ("x match count:" + R. Matches (x). Count); // 1
Console. writeline ("y match count:" + R. Matches (Y). Count); // 0
// The Regular Expression Engine remembers the matched content in "()" as a "group" and can be referenced through indexes. "/1" in the expression is used to reference the first group in the expression in reverse direction, that is, the content of the first bracket marked in bold, and "/2.

String x = "Live for nothing, die for something ";
RegEx r = new RegEx (@ "^ live for no ([A-Z] {5}), die for some/1 $ ");
If (R. ismatch (x ))
{Console. writeline ("group1 value:" + R. Match (x). Groups [1]. Value); // output: thing}
// Obtain the content in the group. Note: This is groups [1], because groups [0] is the entire matching string, that is, the content of the entire variable X.

String x = "Live for nothing, die for something ";
RegEx r = new RegEx (@ "^ live for no (? <G1> [A-Z] {5}), die for some/1 $ ");
If (R. ismatch (x ))
{Console. writeline ("group1 value:" + R. Match (x). Groups ["g1"]. Value);} // output: thing
// Indexes can be performed based on the group name. Use the following format to identify a group name (? <Groupname> ...).

String x = "Live for nothing ";
RegEx r = new RegEx (@ "([A-Z] +)/1 ");
If (R. ismatch (x ))
{X = R. Replace (x, "$1 ");
Console. writeline ("Var X:" + x); // output: Live for nothing}
// Delete the repeated "nothing" in the original string ". In addition to the expression, use "$1" to reference the first group. The group name is used for reference below:
String x = "Live for nothing ";
RegEx r = new RegEx (@"(? <G1> [A-Z] +)/1 ");
If (R. ismatch (x ))
{X = R. Replace (x, "$ {G1 }");
Console. writeline ("Var X:" + x); // output: Live for nothing}

String x = "Live for nothing ";
RegEx r = new RegEx (@ "^ live for no (? : [A-Z] {5}) $ ");
If (R. ismatch (x ))
{Console. writeline ("group1 value:" + R. Match (x). Groups [1]. Value); // output: (null )}
// Add "? : "Indicates that this is a" non-capturing group ", that is, the engine will not save the content of this group.

(8) greedy and non-greedy
The engine of the regular expression is greedy. As long as the mode permits, it will match as many characters as possible. Add "?" after "repeated description characters" (*, +), You can change the matching mode to non-greedy.

String x = "Live for nothing, die for something ";
RegEx R1 = new RegEx (@ ". * thing ");
If (r1.ismatch (x ))
{Console. writeline ("Match:" + r1.match (x). value );

// Output: Live for nothing, die for something}
RegEx r2 = new RegEx (@".*? Thing ");
If (r2.ismatch (x ))
{Console. writeline ("Match:" + r2.match (x). Value); // output: Live for nothing}

(9) backtracking and non-backtracking
Use "(?> ...)" Method. Due to the greedy nature of the Regular Expression Engine, in some cases, it will be traced back to obtain matching

String x = "Live for nothing, die for something ";
RegEx R1 = new RegEx (@ ". * thing ,");
If (r1.ismatch (x ))
{Console. writeline ("Match:" + r1.match (x). Value); // output: Live for nothing ,}
RegEx r2 = new RegEx (@ "(?>. *) Thing ,");
If (r2.ismatch (x) // Mismatch
{Console. writeline ("Match:" + r2.match (x). Value );}
// In R1, ". * "because of its greedy feature, it will always match the end of the string, and then match" thing ", but fails when", ". In this case, the engine will trace back and,. In R2, the entire expression fails to be matched due to forced non-backtracking.

(10) forward and reverse pre-Search

① Forward pre-search declaration format: positive declaration "(? = ...)", Negative statement "(?!...)" The statement itself is not part of the final matching result.

String x = "1024 used 2048 free ";
RegEx R1 = new RegEx (@ "/d {4 }(? = Used )");
If (r1.matches (x). Count = 1)
{Console. writeline ("R1 match:" + r1.match (x). Value); // output: 1024}
RegEx r2 = new RegEx (@ "/d {4 }(?! Used )");
If (r2.matches (x). Count = 1)
{Console. writeline ("R2 match:" + r2.match (x). Value); // output: 2048}
// The positive declaration in R1 indicates that the four digits must be followed by "used". The negative declaration in R2 indicates that the four digits cannot be followed by "used ".

② Reverse pre-search declaration format: positive declaration "(? <=) ", Negative statement" (? <!)", The statement itself is not part of the final matching result.

String x = "used: 1024 free: 2048 ";
RegEx R1 = new RegEx (@"(? <= Used :)/d {4 }");
If (r1.matches (x). Count = 1)
{Console. writeline ("R1 match:" + r1.match (x). Value); // output: 1024}
RegEx r2 = new RegEx (@"(? <! Used :)/d {4 }");
If (r2.matches (x). Count = 1)
{Console. writeline ("R2 match:" + r2.match (x). Value); // output: 2048}
// The Reverse positive declaration in R1 indicates that the four digits must be followed by "used:". The reverse negative declaration in R2 indicates that the four digits must be followed by "used:.

C # system. Text. regularexpressions. RegEx (2)

(11) hexadecimal character range

In a regular expression, you can use "/XXX" and "/uxxxx" to indicate a character range ("X" indicates a hexadecimal number:
The character of the/xxx number in the range of 0 to 255. For example, the space can be expressed by "/x20.
The/uxxxx character can be expressed by "/u" plus the 4-digit hexadecimal number of its number. For example, the Chinese character can be expressed by "[/u4e00-/u9fa5.

(12) A relatively complete matching for [0,100] requires special consideration, including
* 00 legal, 00. Legal, 00.00 legal, 001.100 legal
* The Null String is invalid. Only the decimal point is invalid. The value greater than 100 is invalid.
* The value can be suffixed. For example, "1.07f" indicates that the value is of the float type (not considered)
RegEx r = new RegEx (@ "^/+? 0 *(? : 100 (/. 0 *)? | (/D {0, 2 }(? = //./D) |/d {1, 2 }(? = ($ |/. $) (/./D *)?) $ ");
(13) exact matching is sometimes difficult
In some cases, it is difficult to achieve exact matching, such as date, URL, and email address. In some cases, you even need to study some specialized documents to write accurate and complete expressions. In this case, you can only return to the next step to ensure exact matching. For example, you can consider a short period of time based on the actual situation of the application system, or for email-like matching, you can only consider the most common form.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.