Regular expressions, grouping, child matching (child mode), non-capture child matching (child mode) _ Regular expressions

Source: Internet
Author: User
Tags tag name

We know that the regular expression has a number of metacharacters representing the number of matches (quantifiers) that can be repeated to match the number of individual characters appearing earlier. Sometimes, we may need to match the number of occurrences of a set of multiple characters. This time, we need to group. is to enclose these characters in parentheses, specifying the subexpression (also called grouping). You can then specify the number of repetitions of the subexpression, and you can do some other things with the sub expression. At this point, you can think of a group of characters in parentheses as a whole.

Grouping Mode Example Description

For example, in the lookup string, successive occurrences of a character with multiple win strings. You can finish this.

<?php 
$str = "This is win winwindows!";
Preg_match_all ("/(Win) +/", $str, $marr);
Var_dump ($marr);

Do you want to match multiple characters without grouping mode? We found the previous action symbol: [win]+, although it can be matched to the WinWin character, because it represents one or more characters with a w,i,n combination, does not restrict the order. See it will match up to like: Wwin,www,inw and so on, as long as it is composed of these 3 characters, multiple characters are matched successfully.

How does the above match to, each one has 2 results? This is the child pattern (a child match), by default, in addition to combining multiple characters into a whole, and storing the parentheses, the part of the expression enclosed in a temporary buffer for subsequent regular expression calls. In this example above, we do not need to call later. So how do you block out this subexpression to capture the content? Only need to add "?:" Before:. Let's look at the following example of a regular expression grouping non-capture mode.

What are the advantages of non capture mode? From the above, you can reduce the capture and reduce the number of matches. Therefore, adding the non-capture prefix "?:" in unnecessary grouping expressions can save memory overhead and increase the matching speed!

Just talked about regular expression grouping, by default, the subexpression captures the content and stores it in a buffer. For subsequent calls. So what's the situation? In fact, this is a reference to a regular expression. Each captured child match is stored sequentially according to what is encountered from left to right in the regular expression pattern. The buffer number for the storage child match starts at 1 and can be stored as a maximum of 9. So that the following expression refers to the value, also called a back reference.

Let's look at the following example, look up a string, not adjacent to each other, multiple system words: Add.

<?php
$str = "ADD123456ADDASDF"; 
Preg_match_all ('/(add) \d+\1/', $str, $marr); 
Var_dump ($marr);

A reverse reference is often used to handle some special matching situations. such as: In the lookup string, a duplicate string is not adjacent. Find the contents of an HTML pair of tags. Special analysis of HTML is very common (note that if you use a reverse reference, the front can not shield the child matching capture, that is, can not add "?:" prefix). It is often used:

<?php
$str = file_get_contents (' http://blog.chacuo.net/');
Preg_match_all ('/< (\s+) [^>]*>[^<]*<\/\1>/', $str, $marr);
 
Var_dump ($marr);
 
(\s+) represents all characters other than display characters, generally as HTML tag name
//tag format generally for <tag  other properties > back [^>]* matching tag inside all other attributes
//back [^< ]* represents <tag...> intermediate content </tag> represents intermediate contents to "<" end, so matches all [^<]* character
//final <\/\1> "\/" Escape "/" characters, followed by "\1" Indicates a reverse application (\s+)

The above is a regular expression using the more important, grouping, reverse matching, and non capture grouping instructions and examples. I want to be helpful to the property change function friend. At the same time welcome friends to communicate!

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.