Introduction to Regular Expressions (Microsoft)--13. Selection and Grouping
Source: Internet
Author: User
Selection and Grouping
Select Allow the ' | ' character to be selected in two or more candidates. By extending the regular expression of the chapter title, you can extend it to an expression that applies not only to chapter headings. However, this is not as direct as you might think. When you use a selection, the most likely expression on each side of the ' | ' character is matched. You might think that the following Visual Basic scripting Edition and VBScript expressions will match the ' Chapter ' or ' section ' at the beginning and end of a line and followed by one or two digits:
/^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 line or the ' section ' followed by any number at the end of a line. If the input string is ' Chapter 22 ', the expression above will only match the word ' Chapter '. If the input string is ' section 22 ', the expression will match ' section 22 '. But this result is not our purpose here, so there has to be a way to make regular expressions more responsive to what you want to do, and you do have this approach.
You can use parentheses to limit the range of selections, which means that the selection applies only to the two words ' Chapter ' and ' section '. However, parentheses are also difficult to handle, because they are also used to create subexpression, and some of the content is described later in the section on the subexpression. By taking the regular expression shown above and adding parentheses in place, you can make the regular expression match either ' Chapter 1 ' or ' section 3 '.
The following regular expression uses parentheses to form a group of ' Chapter ' and ' section ' so that the expression works correctly. For Visual Basic scripting Edition is:
/^ (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. In ' chapter| Section ' placed parentheses on both sides establishes the appropriate grouping, but also causes one of the two matching words to be captured for future use. Because there is only one set of parentheses in the expression shown above, there can be only one captured Submatch. You can reference this child match using the Submatches collection of VBScript or the $1-$9 property of the RegExp object in Visual Basic scripting Edition.
Sometimes capturing a child match is desirable and sometimes not desirable. In the example shown in the description, the real thing to do is to use parentheses to group the selection between the word ' Chapter ' or ' section '. You do not want to refer to the match later. In fact, do not use unless you really need to catch a child match. This regular expression is more efficient because it does not take time and memory to store those child matches.
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