Replacement use | character to allow selection between two or more replacement options. For example, you can extend the Chapter title regular expression to return a match that has a wider range than the Chapter title. However, this is not as simple as you might think. Replacement matching | the largest possible expression on both sides of the character. You may think that the following expression matches the chapter or section with one or two numbers at the beginning and end of the row:
/^Chapter|Section [1-9][0-9]{0,1}$/
Unfortunately, the regular expression above either matches the word at the beginning of the line.Chapter, Or match the word at the end of the lineSectionAnd any number following it. If the input string is chapter 22, the above expression only matches words.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 two words.ChapterAndSection. 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}$/
Although these expressions work correctly, the brackets on both sides of Chapter | Section also enable any 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 useRegexpObject$1-$9To reference this sub-match.
In the preceding example, you only need to use parentheses to combine words.ChapterAndSection. 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.