Introduction to regular expressions (Microsoft) -- 13. Selection and grouping

Source: Internet
Author: User

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.

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.