Comprehensive Analysis of Linux Regular Expressions (3)

Source: Internet
Author: User

The pattern modifier and metacharacters of Linux regular expressions have been introduced in the previous two articles, because the existing regular expressions on the Internet have detailed descriptions and many examples, if you feel you are not familiar with the previous section, refer to these materials. This article hopes to involve as many advanced regular expression features as possible.
In this article, we will mainly introduce the subpatterns, Back references, and quantifiers of Linux regular expressions. We will focus on some extended applications of these concepts, for example, in the submode, the non-capturing submode matches the greedy and ungreedy when the quantifiers match.

Subpatterns and Back references)

Regular Expressions can contain multiple character modes. The child mode is bounded by parentheses and can be nested. This is also the role of two metacharacters "(" and. The sub-mode can have the following functions:
1. Select one branch for localization.
For example, if cat (aract | erpillar |) matches one of "cat", "cataract", or "caterpillar", it matches "cataract ", "erpillar" or an empty string.
2. Set the sub-mode to the capture sub-mode (for example, in the above example ). During full mode matching, the part of the target string that matches the sub-mode can be called through reverse reference. The left parentheses are counted from left to right (starting from 1) to obtain the number of capture submodes.
Note that the sub-mode can be nested. For example, if the string "the red king" is matched with the mode/the (red | white) (king | queen, the captured substrings are "red king", "red", and "king", and are counted as 1, 2, and 3. You can use "1", "2 ", "3" to reference them separately. "1" contains "2" and "3", and their sequence numbers are determined by the sequence of left parentheses.
In some old linux/unux tools, parentheses used in the child mode need to be escaped by backslash (subpattern), but modern tools no longer need them, examples used in this article are not escaped.
Non-capturing subpatterns)
When you use a pair of parentheses to complete the two features of the sub-mode mentioned above at the same time, some problems may occur, for example, because the number of reverse references is limited (usually not more than 9 ), in addition, sub-mode definitions that do not need to be captured are often encountered. At this time, you can add question marks and colons after the start brackets to indicate that this submode does not need to be captured, so it is like the following :((? : Red | white) (king | queen )).
If "the white queen" is used as the target string for pattern matching, the captured strings include "white queen" and "queen", respectively as "1" and "2 ", although white complies with the sub-mode "(? : Red | white) ", but not captured.
We have already introduced the method of using parentheses and question marks to represent the pattern modifier. For convenience, if you need to insert a pattern modifier in non-capture submode, you can place it directly between the question mark and the colon. For example, the following two modes are equivalent.
/(? I: Saturday | Sunday) // and /(? :(? I) Saturday | Sunday )/.

Back references)

When we introduced the backslash function, we mentioned that it is used to represent reverse references. When a backslash outside the character class is followed by a decimal number greater than 0, it is likely to be a reverse reference. Its meaning is like its name. It indicates a reference to the subpattern that has been captured before. This number represents the order in which the left parentheses referenced by it appear in the pattern. We have seen an example of reverse referencing when introducing the subpattern, where "1" exists ", "2", "3" respectively indicate the content of the subpattern defined by the first, second, and third parentheses captured.
It is worth noting that when the number after the backslash is less than 10, it can be determined that this is a reverse reference, this reverse reference can appear before a certain number of left parentheses are captured without confusion. Only the entire mode can provide so many capture submodes, so no error is reported. It seems confusing. Let's take a look at the example below. Modify the example given when introducing the sub-model. the string "the red king" is used to match the pattern/the (red | white) (king | queen, the captured substrings are "red king", "red", and "king" and are counted as 1, 2, and 3. Now, modify the string to "king, the red king ", change the mode to/3, the (red | white) (king | queen)/, this mode should also be able to match. However, not all regular expression tools support this usage. The safe practice is to use reverse references after the left parenthesis of the corresponding serial number.
Note that the reverse reference value is a string segment that actually captures the sub-mode in the target string, rather than the sub-mode itself. For example,/(sens | respons) e and 1 ibility/matches "sense and sensibility" and "response and responsibility", but not "sense and responsibility ". When the subpattern to be referenced is followed by a quantifier and therefore repeatedly matched multiple times, the value of the subpattern to be referenced is subject to the value of the last match. For example, when/([abc]) {3}/matches the string "abc", the value of "1" in reverse reference will be the last matched result "c ".

I believe that you have some knowledge about the subpatterns and Back references of Linux regular expressions, and there will be more exciting content later.

  1. Linux Regular Expression 2)
  2. Linux Regular Expression 1)
  3. Describes how to install a Linux virtual machine.
  4. Linux init process and related files
  5. Detailed analysis of ten aspects of Linux Server Security Protection

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.