Regular expressions, which were previously summarized, are mostly used on the Internet.

Source: Internet
Author: User

Backward reference construction
Reverse reference provides a convenient way to find duplicate character groups. They can be considered as a shortcut command that matches the same string again.

Syntax: \ number, number is the group number
(\ W) \ 1, repeat once for a single character
(\ W )(? <Dd> \ d) \ k <dd>
Repeat a single number

Test first does not occupy characters, that is, after a match occurs, the next matched search follows the previous match, rather than the characters that constitute the prediction first.
.

^ Chapter | Section [1-9] [0-9] {0, 1} $/
Unfortunately, the above regular expression either matches the word Chapter at the beginning of the line, or matches the word Section at the end of the line and any number following it. If the input string is Chapter 22, the above expression only matches the word Chapter. If the input string is Section 22, the expression matches Section 22.

To make the regular expression easier to control, you can use parentheses to limit the replacement range, that is, make sure it applies only to the two words Chapter and Section. However, parentheses are also used to create subexpressions and capture them for future use. This is described in the section on reverse references. By adding parentheses at the proper position of the above regular expression, you can make the regular expression match Chapter 1 or Section 3.

The following regular expression uses parentheses to combine Chapter and Section so that the expression works correctly:

/^ (Chapter | Section) [1-9] [0-9] {0, 1} $/
In the preceding example, you only need to use parentheses to combine the choice between the word Chapter and Section. To prevent matching from being saved for future use, place it before the regular expression pattern in parentheses? :. The following modifications provide the same capabilities without saving the child matching items:

/^ (? : Chapter | Section) [1-9] [0-9] {0, 1} $/

 

The following regular expression uses parentheses to combine Chapter and Section so that the expression works correctly:

/^ (Chapter | Section) [1-9] [0-9] {0, 1} $/
Although these expressions work correctly, the parentheses on both sides of Chapter | Section also cause one of the two matching words to be captured for future use. Since there is only one set of parentheses in the above expression, there is only one "sub-match" to be captured ". You can reference this submatch by using the $1-$9 attribute of the RegExp object.

In the preceding example, you only need to use parentheses to combine the choice between the word Chapter and Section. To prevent matching from being saved for future use, place it before the regular expression pattern in parentheses? :. The following modifications provide the same capabilities without saving the child matching items:

/^ (? : Chapter | Section) [1-9] [0-9] {0, 1} $/
Division? : Except for metacharacters, two other non-capturing metacharacters are created to be called "prediction first" matching. Is Forward prediction used first? = Specified. It matches the search string that is in parentheses and matches the start point of the regular expression pattern. Is reverse prediction used first ?! It matches the search string at the start point of the string that does not match the regular expression pattern.

For example, assume that you have a document that contains references to Windows 3.1, Windows 95, Windows 98, and Windows NT. Further assume that you need to update this document to change all references pointing to Windows 95, Windows 98, and Windows NT to Windows 2000. The following regular expression (this is an example of positive prediction first) matches Windows 95, Windows 98, and Windows NT:

/Windows (? = 95 | 98 | NT )/
After finding a match, search for the next match in the matched text (excluding the characters in the prediction first. For example, if the expression above matches Windows 98, the search will continue after Windows instead of after 98.

 

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.