Select and group
Select the '|' character to select two or more candidate items. By extending the regular expression of a Chapter title, you can expand it to an expression not only applicable to the Chapter title. However, this is not as straightforward as imagined. When you use the selection option, the '|' character is matched with the most likely expression on each side. You may think that the following Visual Basic Scripting Edition and VBScript expressions will match the 'Chapter 'or 'secte' at the beginning and end of a row followed by one or two numbers ':
/^ Chapter | Section [1-9] [0-9] {0, 1} $/
"^ Chapter | Section [1-9] [0-9] {0, 1} $"
Unfortunately, the real situation is that the regular expression shown above either matches the word 'Chapter 'at the beginning of a row, or matches the 'core' at the end of a row followed by any number '. If the input string is 'Chapter 22 ', the above expression will only match the word 'Chapter '. If the input string is 'section 22', the expression matches 'section 22 '. However, this result is not our purpose here, so there must be a way to make the regular expression easier to respond to, and there is indeed this method.
You can use parentheses to limit the selection range. That is to say, it is clear that the selection only applies to the two words 'Chapter 'and 'section '. However, parentheses are also hard to handle because they are also used to create subexpressions. Some content will be described later in the section about subexpressions. By using the regular expression shown above and adding parentheses at the appropriate position, you can make the regular expression match 'Chapter 1' or 'section 3 '.
The following regular expression uses parentheses to form a group of 'Chapter 'and 'section', so that the expression can work correctly. For Visual Basic Scripting Edition:
/^ (Chapter | Section) [1-9] [0-9] {0, 1} $/
For VBScript:
"^ (Chapter | Section) [1-9] [0-9] {0, 1} $"
These expressions work correctly, but produce an interesting by-product. An appropriate group is created by placing parentheses on both sides of 'Chapter | Section ', which also results in the capturing of one of the two words to be matched for future use. As the expression shown above only contains a set of parentheses, only one submatch can be captured. You can use the Submatches set of VBScript or the $1-$9 attribute of the RegExp object in Visual Basic Scripting Edition to reference this submatch.
Sometimes it is expected to capture a child match, but sometimes it is not expected. In the example shown in the description, you really want to use parentheses to select a group between the words 'Chapter 'or 'section. You do not want to reference this match later. In fact, do not use it unless you really need to capture the child match. Because you do not need to spend time and memory to store those child matches, this regular expression is more efficient.