Description of Capture group/non-capture group of regular expressions _ regular expressions

Source: Internet
Author: User
Tags readline
Capturing groups
Grammar:

character  

Describe

Example

(pattern)

Matches the pattern and captures the result, setting the group number automatically.

(ABC) +d

Match ABCD or ABCABCD

(?<name>pattern)

Or

(?' name'pattern ')

Matches the pattern and captures the result, setting name to the group name.

\Num

A reverse reference to the capturing group. Where num is a positive integer.

(\w) (\w) \2\1

Matching ABBA

\k< name >

Or

\k ' name '

A reverse reference to a named capture group. Where name is the capturing group name.

(? <group>\w) abc\k<group>

Matching XABCX

After you use parentheses to specify a subexpression, the text that matches the subexpression (that is, what this group captures) can be further processed in an expression or other program. By default, each capturing group automatically owns a group number, which is left to right, is marked with the left parenthesis of the group, the first group that appears is 1, the second is 2, and so on.
For example:
(\d{4})-(\d{2}-(\d{2}))
1 1 2 3 32
The following is an example of a program that handles capturing groups, resolves a URL address, and displays all capturing groups.
You can see the capturing group number set sequentially.
Regex.match method
Copy Code code as follows:

Using System.Text.RegularExpressions;
Namespace Wuhong.test
{
Class Program
{
static void Main (string[] args)
{
Target string
String Source = "http://reg-test-server:8080/download/file1.html#";
Regular type
string regex = @ "(\w+): \/\/([^/:]+) (: \d+)? ([^# :]*)";
Regex regurl = new regex (regex);
Matching regular expressions
Match m = regurl.match (source);
Console.WriteLine (m.success);
if (m.success)
{
Capturing groups are stored in the Match.Groups collection with index values starting at 1, and index 0 to match the entire string value
Display as "group number: Capture Content" format
for (int i = 0; i < M.groups.count; i++)
{
Console.WriteLine (String. Format ("{0}: {1}", I, M.groups[i]));
}
}
Console.ReadLine ();
}
}
}

You can also specify the group name of the subexpression yourself. This allows you to refer directly to the group name in an expression or program, or you can continue to use the group number. However, if both common and named capture groups are present in the regular expression, the number of the capturing group should be paid special attention, and the number rule is to number the common capturing group first and then the named Capture Group.
For example:
(\d{4})-(? <date>\d{2}-(\d{2}))
1 1 3 2 23

The following handles the named capture group in the program, displays the group number generated by the blending rule, and replaces the source string with the contents of the capturing group.
You can see that the normal capture group is numbered first, and then the named Capture group is numbered.
Regex.Replace method
Copy Code code as follows:

Using System.Text.RegularExpressions;
Namespace Wuhong.test
{
Class Program
{
static void Main (string[] args)
{
Target string
String Source = "http://reg-test-server:8080/download/file1.html#";
Regular, naming two of the groups
string regex = @ "(\w+): \/\/(? <server>[^/:]+) (? <port>:\d+)? ([^# :]*)";
Regex regurl = new regex (regex);
Matching regular expressions
Match m = regurl.match (source);
Console.WriteLine (m.success);
if (m.success)
{
Capturing groups are stored in the Match.Groups collection with index values starting at 1, and index 0 to match the entire string value
Display as "group number: Capture Content" format
for (int i = 0; i < M.groups.count; i++)
{
Console.WriteLine (String. Format ("{0}: {1}", I, M.groups[i]));
}
}
Replace string
The "$ group number" reference captures the contents of the group.
Special attention should be paid to "$ group number" can not be followed by the number of strings, if this situation, you need to use a named capture group, referencing the format "${Group name}"
string replacement = string. Format ("$1://{0}{1}$2", "New-reg-test-server", "");
string result = Regurl.replace (source, replacement);
Console.WriteLine (result);
Console.ReadLine ();
}
}
}


non-capture group
Grammar:

character  

Describe

Example

(?:pattern)

Matches pattern but does not capture the result of the match.

' Industr (?: y|ies)

Match ' industry ' or ' industries '.

(? =pattern)

0-Width forward check without capturing the matching result.

' Windows (? =95|98| nt|2000) '

Match Windows in "Windows2000"

Does not match "Windows" in "Windows3.1".

(?! pattern)

0-width Negative pre-check, no match results captured.

' Windows (?! 95|98| nt|2000) '

Match Windows in "Windows3.1"

Does not match "Windows" in "Windows2000".

(? <=pattern)

0-Width forward callback without capturing the matching result.

' <=office| (? word| Excel) '

Match "2000" in "Office2000"

Does not match "2000" in "Windows2000".

(? <! pattern)

0-width negative return, do not capture match results.

' <! (? office| word| Excel) '

Match "2000" in "Windows2000"

Does not match "2000" in "Office2000".


Non-capture groups match only the results, but do not capture the results, nor assign group numbers, and of course do not do further processing in expressions and programs.
The first (:p Attern) differs from (pattern) simply by not capturing the result.
The next four non-capture groups are used to match the content before (or after) the pattern (or unmatched pattern) position. The result of the match does not include pattern.
For example:
(?<=< (\w+) >). * (?=<\/\1>) matches the contents of a simple HTML tag that does not contain attributes. such as Hello in:<div>hello</div>, the matching result does not include prefix <div> and suffix </div>.
The following is an example of a non-capturing group in the program, used to extract the ZIP code.
You can see that reverse lookup and reverse check are not captured.
Regex.Matches method
Copy Code code as follows:

Using System.Text.RegularExpressions;
Namespace Wuhong.test
{
Class Program
{
static void Main (string[] args)
{
Target string
String Source = "There are 6 sets of numbers: 010001,100,21000,310000,4100011,510002, pick up the postcode. ";
Regular type
string regex = @ "(? <!\d) ([1-9]\d{5}) (?! \d) ";
Regex regurl = new regex (regex);
Get all Matches
MatchCollection mlist = regurl.matches (source);
for (int j = 0; J < Mlist.count; J + +)
{
Show each group, and you can see that each group has only a group number of 1 items, reverse lookup and reverse lookup is not captured
for (int i = 0; i < mlist[j]. Groups.count; i++)
{
Console.WriteLine (String. Format (' {0}: {1}: {2} ', J, I, Mlist[j]. Groups[i]));
}
}
Console.ReadLine ();
}
}
}


Comments
Grammar:

Character

Describe

Example

(? #Comment)

Comment is a comment and does not have any effect on the processing of regular expressions

2[0-4]\d (? #200 -249) |25[0-5] (? #250 -255) |1?\d\d? (? #0-199)

Match 0-255 of integers

This does not explain.

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.